Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Machine Conditions #11

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ endif
### These variables should not need tweaking.
###

SRC_PKGS := api cmd crds # directories which hold app source excluding tests (not vendored)
SRC_PKGS := api cmd crds pkg # directories which hold app source excluding tests (not vendored)
SRC_DIRS := $(SRC_PKGS) # directories which hold app source (not vendored)

DOCKER_PLATFORMS := linux/amd64 linux/arm64
Expand Down
42 changes: 20 additions & 22 deletions api/v1alpha1/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,21 @@ import (
type MachinePhase string

const (
MachineConditionTypeMachineReady kmapi.ConditionType = "MachineReady"
MachineConditionTypeScriptReady kmapi.ConditionType = "ScriptReady"
MachineConditionTypeAuthDataReady kmapi.ConditionType = "AuthDataReady"
MachineConditionTypeScriptComplete kmapi.ConditionType = "ScriptComplete"
MachineConditionTypeMachineReady kmapi.ConditionType = "MachineReady"
MachineConditionTypeScriptReady kmapi.ConditionType = "ScriptReady"
MachineConditionTypeAuthDataReady kmapi.ConditionType = "AuthDataReady"
MachineConditionTypeClusterOperationComplete kmapi.ConditionType = "ClusterOperationComplete"
MachineConditionTypeMachineCreating kmapi.ConditionType = "MachineCreating"
)

const (
MachineConditionClusterOperationSuccessful = "ClusterOperationSuccessful"
MachineConditionClusterOperationFailed = "ClusterOperationFailed"
MachineConditionWaitingForScriptCompletion = "WaitingForScriptCompletion"
MachineConditionAuthDataNotFound = "AuthDataNotFound"
MachineConditionScriptDataNotFound = "ScriptDataNotFound"
MachineConditionMachineCreating = "MachineCreating"
ReasonClusterOperationFailed = "ClusterOperationFailed"
ReasonMachineCreationFailed = "MachineCreationFailed"
ReasonWaitingForScriptCompletion = "WaitingForScriptCompletion"
ReasonWaitingForScriptRun = "WaitingForScriptRun"
ReasonAuthDataNotFound = "AuthDataNotFound"
ReasonScriptDataNotFound = "ScriptDataNotFound"
ReasonMachineCreating = "MachineCreating"
)

const (
Expand All @@ -53,8 +55,8 @@ const (

func ConditionsOrder() []kmapi.ConditionType {
return []kmapi.ConditionType{
MachineConditionTypeScriptComplete,
MachineConditionTypeMachineReady,
MachineConditionTypeClusterOperationComplete,
MachineConditionTypeAuthDataReady,
MachineConditionTypeScriptReady,
}
Expand Down Expand Up @@ -85,20 +87,16 @@ func GetPhase(obj *Machine) MachinePhase {
return MachinePhaseSuccess
}

if cond.Reason == MachineConditionAuthDataNotFound ||
cond.Reason == MachineConditionScriptDataNotFound {
return MachinePhaseInProgress
}
if cond.Reason == MachineConditionMachineCreating {
return MachinePhaseInProgress
}
if cond.Reason == MachineConditionWaitingForScriptCompletion {
if cond.Reason == ReasonWaitingForScriptCompletion {
return MachinePhaseWaitingForScriptCompletion
}
if cond.Reason == MachineConditionClusterOperationFailed {
return MachineConditionClusterOperationFailed
if cond.Reason == ReasonClusterOperationFailed {
return MachinePhaseClusterOperationFailed
}
if cond.Reason == ReasonMachineCreationFailed {
return MachinePhaseFailed
}
return MachinePhaseFailed
return MachinePhaseInProgress
}

func GetFinalizer() string {
Expand Down
31 changes: 31 additions & 0 deletions api/v1alpha1/machine_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright AppsCode Inc. and Contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import kmapi "kmodules.xyz/client-go/api/v1"

func (in *Machine) GetStatus() *MachineStatus {
return &in.Status
}

func (in *Machine) GetConditions() kmapi.Conditions {
return in.Status.Conditions
}

func (in *Machine) SetConditions(conditions kmapi.Conditions) {
in.Status.Conditions = conditions
}
19 changes: 7 additions & 12 deletions api/v1alpha1/machine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ import (
kmapi "kmodules.xyz/client-go/api/v1"
)

const (
ResourceCodeMachine = "mc"
ResourceKindMachine = "Machine"
ResourceSingularMachine = "machine"
ResourcePluralMachine = "machines"
)

// MachineSpec defines the desired state of Machine
type MachineSpec struct {
Driver *core.LocalObjectReference `json:"driver"`
Expand Down Expand Up @@ -69,15 +76,3 @@ type MachineList struct {
func init() {
SchemeBuilder.Register(&Machine{}, &MachineList{})
}

func (in *Machine) GetStatus() *MachineStatus {
return &in.Status
}

func (in *Machine) GetConditions() kmapi.Conditions {
return in.Status.Conditions
}

func (in *Machine) SetConditions(conditions kmapi.Conditions) {
in.Status.Conditions = conditions
}
7 changes: 3 additions & 4 deletions pkg/cmds/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ package cmds
import (
"context"

"github.com/spf13/cobra"
api "go.klusters.dev/docker-machine-operator/api/v1alpha1"
"go.klusters.dev/docker-machine-operator/pkg/cmds/server"

"github.com/spf13/cobra"
v "gomodules.xyz/x/version"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand All @@ -30,9 +31,7 @@ import (
"k8s.io/klog/v2"
)

var (
scheme = runtime.NewScheme()
)
var scheme = runtime.NewScheme()

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
Expand Down
18 changes: 10 additions & 8 deletions pkg/cmds/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (
"os"
"time"

"github.com/spf13/pflag"
dockermachinev1alpha1 "go.klusters.dev/docker-machine-operator/api/v1alpha1"
"go.klusters.dev/docker-machine-operator/pkg/controller"

"github.com/spf13/pflag"
v "gomodules.xyz/x/version"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand All @@ -41,8 +42,10 @@ import (
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
)

var setupLog = log.Log.WithName("setup")
var scheme = runtime.NewScheme()
var (
setupLog = log.Log.WithName("setup")
scheme = runtime.NewScheme()
)

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
Expand Down Expand Up @@ -106,7 +109,6 @@ func (s *OperatorOptions) AddGoFlags(fs *flag.FlagSet) {
}

func (s OperatorOptions) Run(ctx context.Context) error {

klog.Infof("Starting binary version %s+%s ...", v.Version.Version, v.Version.CommitHash)

ctrl.SetLogger(klogr.New()) // nolint:staticcheck
Expand Down Expand Up @@ -138,15 +140,15 @@ func (s OperatorOptions) Run(ctx context.Context) error {
}

if err = (&controller.DriverReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
KBClient: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Driver")
os.Exit(1)
}
if err = (&controller.MachineReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
KBClient: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Machine")
os.Exit(1)
Expand Down
33 changes: 19 additions & 14 deletions pkg/controller/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package controller

import (
"context"
"errors"
"fmt"
"strings"
Expand All @@ -39,7 +38,7 @@ const (
awsInternetGatewayIDAnnotation = "docker-machine-operator/aws-gateway"
awsVpcCIDR = "10.1.0.0/16"
allowAllIPs = "0.0.0.0/0"
defaultZone = "a" //same as rancher amazonec2 driver default zone
defaultZone = "a" // same as rancher amazonec2 driver default zone
regionParameter = "amazonec2-region"
)

Expand All @@ -62,16 +61,16 @@ func (r *MachineReconciler) getAnnotationsArgsForAWS() []string {
return annotationArgs
}

func (r *MachineReconciler) cleanupAWSResources(ctx context.Context) error {
c, err := r.awsEC2Client(ctx)
func (r *MachineReconciler) cleanupAWSResources() error {
c, err := r.awsEC2Client()
if err != nil {
return err
}
return r.deleteAwsVpc(c, r.machineObj.Annotations[awsVPCIDAnnotation])
}

func (r *MachineReconciler) getAWSCredentials(ctx context.Context) (*awsAuthCredential, error) {
authSecret, err := r.getSecret(ctx, r.machineObj.Spec.AuthSecret)
func (r *MachineReconciler) getAWSCredentials() (*awsAuthCredential, error) {
authSecret, err := r.getSecret(r.machineObj.Spec.AuthSecret)
if err != nil {
return nil, err
}
Expand All @@ -91,8 +90,8 @@ func (r *MachineReconciler) getAWSCredentials(ctx context.Context) (*awsAuthCred
return &awsCreds, nil
}

func (r *MachineReconciler) newAWSClientSession(ctx context.Context) (*session.Session, error) {
cred, err := r.getAWSCredentials(ctx)
func (r *MachineReconciler) newAWSClientSession() (*session.Session, error) {
cred, err := r.getAWSCredentials()
if err != nil {
return nil, err
}
Expand All @@ -109,8 +108,8 @@ func (r *MachineReconciler) newAWSClientSession(ctx context.Context) (*session.S
return session, nil
}

func (r *MachineReconciler) awsEC2Client(ctx context.Context) (*ec2.EC2, error) {
sess, err := r.newAWSClientSession(ctx)
func (r *MachineReconciler) awsEC2Client() (*ec2.EC2, error) {
sess, err := r.newAWSClientSession()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -144,6 +143,7 @@ func createAwsRoute(c *ec2.EC2, vpcId, internetGatewayID string) error {
klog.Infof("route table updated")
return nil
}

func deleteAwsRoute(c *ec2.EC2, vpcId string) error {
out, err := c.DescribeRouteTables(&ec2.DescribeRouteTablesInput{})
if err != nil {
Expand Down Expand Up @@ -177,6 +177,7 @@ func attachInternetGatewayToVPC(c *ec2.EC2, internetGatewayId, vpcID string) err
})
return err
}

func detachInternetGatewayToVPC(c *ec2.EC2, internetGatewayID, vpcID string) error {
_, err := c.DetachInternetGateway(&ec2.DetachInternetGatewayInput{
InternetGatewayId: &internetGatewayID,
Expand Down Expand Up @@ -215,6 +216,7 @@ func (r *MachineReconciler) createAwsInternetGateway(c *ec2.EC2, vpcId string) e
klog.Infof("internet gateway is created with id: %s", *out.InternetGateway.InternetGatewayId)
return nil
}

func deleteAwsInternetGateway(c *ec2.EC2, gatewayId, vpcId string) error {
if err := deleteAwsRoute(c, vpcId); err != nil {
klog.Warningf("failed to delete route, %s", err.Error())
Expand Down Expand Up @@ -263,9 +265,10 @@ func (r *MachineReconciler) createAwsSubnet(c *ec2.EC2, vpcID string) error {
return err
}

r.log.Info("aws subnet created", "subnet id ", *out.Subnet.SubnetId)
r.Log.Info("aws subnet created", "subnet id ", *out.Subnet.SubnetId)
return nil
}

func (r *MachineReconciler) deleteAwsSubnet(c *ec2.EC2, subnetId string) error {
if r.machineObj.Annotations[awsInternetGatewayIDAnnotation] != "" {
if err := deleteAwsInternetGateway(c, r.machineObj.Annotations[awsInternetGatewayIDAnnotation], r.machineObj.Annotations[awsVPCIDAnnotation]); err != nil {
Expand All @@ -283,7 +286,7 @@ func (r *MachineReconciler) deleteAwsSubnet(c *ec2.EC2, subnetId string) error {
return err
}

r.log.Info("subnet successfully deleted")
r.Log.Info("subnet successfully deleted")
return nil
}

Expand All @@ -302,6 +305,7 @@ func getVPC(c *ec2.EC2, vpcId *string) (*ec2.Vpc, error) {
}
return nil, fmt.Errorf("no vpc found with id %s", *vpcId)
}

func (r *MachineReconciler) createAwsVpc(c *ec2.EC2) error {
var vpc *ec2.Vpc
var err error
Expand Down Expand Up @@ -356,6 +360,7 @@ func (r *MachineReconciler) createAwsVpc(c *ec2.EC2) error {
klog.Infof("aws vpc created with id %s", *vpc.VpcId)
return nil
}

func (r *MachineReconciler) deleteAwsVpc(c *ec2.EC2, vpcID string) error {
if r.machineObj.Annotations[awsVPCIDAnnotation] == "" {
return nil
Expand Down Expand Up @@ -410,12 +415,12 @@ func deleteSecurityGroup(c *ec2.EC2, vpcId string) error {
return nil
}

func (r *MachineReconciler) createAWSEnvironment(ctx context.Context) error {
func (r *MachineReconciler) createAWSEnvironment() error {
if r.machineObj.Annotations[awsVPCIDAnnotation] != "" && r.machineObj.Annotations[awsSubnetIDAnnotation] != "" && r.machineObj.Annotations[awsInternetGatewayIDAnnotation] != "" {
return nil
}

c, err := r.awsEC2Client(ctx)
c, err := r.awsEC2Client()
if err != nil {
return err
}
Expand Down
Loading
Loading