Skip to content

Commit 22e6fc2

Browse files
Update Machine Conditions (#11)
Signed-off-by: AbdullahAlShaad <abdullah.alshaad@appscode.com>
1 parent 81a8791 commit 22e6fc2

File tree

14 files changed

+290
-194
lines changed

14 files changed

+290
-194
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ endif
5656
### These variables should not need tweaking.
5757
###
5858

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

6262
DOCKER_PLATFORMS := linux/amd64 linux/arm64

api/v1alpha1/helper.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,21 @@ import (
2626
type MachinePhase string
2727

2828
const (
29-
MachineConditionTypeMachineReady kmapi.ConditionType = "MachineReady"
30-
MachineConditionTypeScriptReady kmapi.ConditionType = "ScriptReady"
31-
MachineConditionTypeAuthDataReady kmapi.ConditionType = "AuthDataReady"
32-
MachineConditionTypeScriptComplete kmapi.ConditionType = "ScriptComplete"
29+
MachineConditionTypeMachineReady kmapi.ConditionType = "MachineReady"
30+
MachineConditionTypeScriptReady kmapi.ConditionType = "ScriptReady"
31+
MachineConditionTypeAuthDataReady kmapi.ConditionType = "AuthDataReady"
32+
MachineConditionTypeClusterOperationComplete kmapi.ConditionType = "ClusterOperationComplete"
33+
MachineConditionTypeMachineCreating kmapi.ConditionType = "MachineCreating"
3334
)
3435

3536
const (
36-
MachineConditionClusterOperationSuccessful = "ClusterOperationSuccessful"
37-
MachineConditionClusterOperationFailed = "ClusterOperationFailed"
38-
MachineConditionWaitingForScriptCompletion = "WaitingForScriptCompletion"
39-
MachineConditionAuthDataNotFound = "AuthDataNotFound"
40-
MachineConditionScriptDataNotFound = "ScriptDataNotFound"
41-
MachineConditionMachineCreating = "MachineCreating"
37+
ReasonClusterOperationFailed = "ClusterOperationFailed"
38+
ReasonMachineCreationFailed = "MachineCreationFailed"
39+
ReasonWaitingForScriptCompletion = "WaitingForScriptCompletion"
40+
ReasonWaitingForScriptRun = "WaitingForScriptRun"
41+
ReasonAuthDataNotFound = "AuthDataNotFound"
42+
ReasonScriptDataNotFound = "ScriptDataNotFound"
43+
ReasonMachineCreating = "MachineCreating"
4244
)
4345

4446
const (
@@ -53,8 +55,8 @@ const (
5355

5456
func ConditionsOrder() []kmapi.ConditionType {
5557
return []kmapi.ConditionType{
56-
MachineConditionTypeScriptComplete,
5758
MachineConditionTypeMachineReady,
59+
MachineConditionTypeClusterOperationComplete,
5860
MachineConditionTypeAuthDataReady,
5961
MachineConditionTypeScriptReady,
6062
}
@@ -85,20 +87,16 @@ func GetPhase(obj *Machine) MachinePhase {
8587
return MachinePhaseSuccess
8688
}
8789

88-
if cond.Reason == MachineConditionAuthDataNotFound ||
89-
cond.Reason == MachineConditionScriptDataNotFound {
90-
return MachinePhaseInProgress
91-
}
92-
if cond.Reason == MachineConditionMachineCreating {
93-
return MachinePhaseInProgress
94-
}
95-
if cond.Reason == MachineConditionWaitingForScriptCompletion {
90+
if cond.Reason == ReasonWaitingForScriptCompletion {
9691
return MachinePhaseWaitingForScriptCompletion
9792
}
98-
if cond.Reason == MachineConditionClusterOperationFailed {
99-
return MachineConditionClusterOperationFailed
93+
if cond.Reason == ReasonClusterOperationFailed {
94+
return MachinePhaseClusterOperationFailed
95+
}
96+
if cond.Reason == ReasonMachineCreationFailed {
97+
return MachinePhaseFailed
10098
}
101-
return MachinePhaseFailed
99+
return MachinePhaseInProgress
102100
}
103101

104102
func GetFinalizer() string {

api/v1alpha1/machine_helper.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright AppsCode Inc. and Contributors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import kmapi "kmodules.xyz/client-go/api/v1"
20+
21+
func (in *Machine) GetStatus() *MachineStatus {
22+
return &in.Status
23+
}
24+
25+
func (in *Machine) GetConditions() kmapi.Conditions {
26+
return in.Status.Conditions
27+
}
28+
29+
func (in *Machine) SetConditions(conditions kmapi.Conditions) {
30+
in.Status.Conditions = conditions
31+
}

api/v1alpha1/machine_types.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ import (
2222
kmapi "kmodules.xyz/client-go/api/v1"
2323
)
2424

25+
const (
26+
ResourceCodeMachine = "mc"
27+
ResourceKindMachine = "Machine"
28+
ResourceSingularMachine = "machine"
29+
ResourcePluralMachine = "machines"
30+
)
31+
2532
// MachineSpec defines the desired state of Machine
2633
type MachineSpec struct {
2734
Driver *core.LocalObjectReference `json:"driver"`
@@ -69,15 +76,3 @@ type MachineList struct {
6976
func init() {
7077
SchemeBuilder.Register(&Machine{}, &MachineList{})
7178
}
72-
73-
func (in *Machine) GetStatus() *MachineStatus {
74-
return &in.Status
75-
}
76-
77-
func (in *Machine) GetConditions() kmapi.Conditions {
78-
return in.Status.Conditions
79-
}
80-
81-
func (in *Machine) SetConditions(conditions kmapi.Conditions) {
82-
in.Status.Conditions = conditions
83-
}

pkg/cmds/run.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ package cmds
1919
import (
2020
"context"
2121

22-
"github.com/spf13/cobra"
2322
api "go.klusters.dev/docker-machine-operator/api/v1alpha1"
2423
"go.klusters.dev/docker-machine-operator/pkg/cmds/server"
24+
25+
"github.com/spf13/cobra"
2526
v "gomodules.xyz/x/version"
2627
"k8s.io/apimachinery/pkg/runtime"
2728
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -30,9 +31,7 @@ import (
3031
"k8s.io/klog/v2"
3132
)
3233

33-
var (
34-
scheme = runtime.NewScheme()
35-
)
34+
var scheme = runtime.NewScheme()
3635

3736
func init() {
3837
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

pkg/cmds/server/server.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ import (
2222
"os"
2323
"time"
2424

25-
"github.com/spf13/pflag"
2625
dockermachinev1alpha1 "go.klusters.dev/docker-machine-operator/api/v1alpha1"
2726
"go.klusters.dev/docker-machine-operator/pkg/controller"
27+
28+
"github.com/spf13/pflag"
2829
v "gomodules.xyz/x/version"
2930
"k8s.io/apimachinery/pkg/runtime"
3031
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -41,8 +42,10 @@ import (
4142
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
4243
)
4344

44-
var setupLog = log.Log.WithName("setup")
45-
var scheme = runtime.NewScheme()
45+
var (
46+
setupLog = log.Log.WithName("setup")
47+
scheme = runtime.NewScheme()
48+
)
4649

4750
func init() {
4851
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
@@ -106,7 +109,6 @@ func (s *OperatorOptions) AddGoFlags(fs *flag.FlagSet) {
106109
}
107110

108111
func (s OperatorOptions) Run(ctx context.Context) error {
109-
110112
klog.Infof("Starting binary version %s+%s ...", v.Version.Version, v.Version.CommitHash)
111113

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

140142
if err = (&controller.DriverReconciler{
141-
Client: mgr.GetClient(),
142-
Scheme: mgr.GetScheme(),
143+
KBClient: mgr.GetClient(),
144+
Scheme: mgr.GetScheme(),
143145
}).SetupWithManager(mgr); err != nil {
144146
setupLog.Error(err, "unable to create controller", "controller", "Driver")
145147
os.Exit(1)
146148
}
147149
if err = (&controller.MachineReconciler{
148-
Client: mgr.GetClient(),
149-
Scheme: mgr.GetScheme(),
150+
KBClient: mgr.GetClient(),
151+
Scheme: mgr.GetScheme(),
150152
}).SetupWithManager(mgr); err != nil {
151153
setupLog.Error(err, "unable to create controller", "controller", "Machine")
152154
os.Exit(1)

pkg/controller/aws.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package controller
1818

1919
import (
20-
"context"
2120
"errors"
2221
"fmt"
2322
"strings"
@@ -39,7 +38,7 @@ const (
3938
awsInternetGatewayIDAnnotation = "docker-machine-operator/aws-gateway"
4039
awsVpcCIDR = "10.1.0.0/16"
4140
allowAllIPs = "0.0.0.0/0"
42-
defaultZone = "a" //same as rancher amazonec2 driver default zone
41+
defaultZone = "a" // same as rancher amazonec2 driver default zone
4342
regionParameter = "amazonec2-region"
4443
)
4544

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

65-
func (r *MachineReconciler) cleanupAWSResources(ctx context.Context) error {
66-
c, err := r.awsEC2Client(ctx)
64+
func (r *MachineReconciler) cleanupAWSResources() error {
65+
c, err := r.awsEC2Client()
6766
if err != nil {
6867
return err
6968
}
7069
return r.deleteAwsVpc(c, r.machineObj.Annotations[awsVPCIDAnnotation])
7170
}
7271

73-
func (r *MachineReconciler) getAWSCredentials(ctx context.Context) (*awsAuthCredential, error) {
74-
authSecret, err := r.getSecret(ctx, r.machineObj.Spec.AuthSecret)
72+
func (r *MachineReconciler) getAWSCredentials() (*awsAuthCredential, error) {
73+
authSecret, err := r.getSecret(r.machineObj.Spec.AuthSecret)
7574
if err != nil {
7675
return nil, err
7776
}
@@ -91,8 +90,8 @@ func (r *MachineReconciler) getAWSCredentials(ctx context.Context) (*awsAuthCred
9190
return &awsCreds, nil
9291
}
9392

94-
func (r *MachineReconciler) newAWSClientSession(ctx context.Context) (*session.Session, error) {
95-
cred, err := r.getAWSCredentials(ctx)
93+
func (r *MachineReconciler) newAWSClientSession() (*session.Session, error) {
94+
cred, err := r.getAWSCredentials()
9695
if err != nil {
9796
return nil, err
9897
}
@@ -109,8 +108,8 @@ func (r *MachineReconciler) newAWSClientSession(ctx context.Context) (*session.S
109108
return session, nil
110109
}
111110

112-
func (r *MachineReconciler) awsEC2Client(ctx context.Context) (*ec2.EC2, error) {
113-
sess, err := r.newAWSClientSession(ctx)
111+
func (r *MachineReconciler) awsEC2Client() (*ec2.EC2, error) {
112+
sess, err := r.newAWSClientSession()
114113
if err != nil {
115114
return nil, err
116115
}
@@ -144,6 +143,7 @@ func createAwsRoute(c *ec2.EC2, vpcId, internetGatewayID string) error {
144143
klog.Infof("route table updated")
145144
return nil
146145
}
146+
147147
func deleteAwsRoute(c *ec2.EC2, vpcId string) error {
148148
out, err := c.DescribeRouteTables(&ec2.DescribeRouteTablesInput{})
149149
if err != nil {
@@ -177,6 +177,7 @@ func attachInternetGatewayToVPC(c *ec2.EC2, internetGatewayId, vpcID string) err
177177
})
178178
return err
179179
}
180+
180181
func detachInternetGatewayToVPC(c *ec2.EC2, internetGatewayID, vpcID string) error {
181182
_, err := c.DetachInternetGateway(&ec2.DetachInternetGatewayInput{
182183
InternetGatewayId: &internetGatewayID,
@@ -215,6 +216,7 @@ func (r *MachineReconciler) createAwsInternetGateway(c *ec2.EC2, vpcId string) e
215216
klog.Infof("internet gateway is created with id: %s", *out.InternetGateway.InternetGatewayId)
216217
return nil
217218
}
219+
218220
func deleteAwsInternetGateway(c *ec2.EC2, gatewayId, vpcId string) error {
219221
if err := deleteAwsRoute(c, vpcId); err != nil {
220222
klog.Warningf("failed to delete route, %s", err.Error())
@@ -263,9 +265,10 @@ func (r *MachineReconciler) createAwsSubnet(c *ec2.EC2, vpcID string) error {
263265
return err
264266
}
265267

266-
r.log.Info("aws subnet created", "subnet id ", *out.Subnet.SubnetId)
268+
r.Log.Info("aws subnet created", "subnet id ", *out.Subnet.SubnetId)
267269
return nil
268270
}
271+
269272
func (r *MachineReconciler) deleteAwsSubnet(c *ec2.EC2, subnetId string) error {
270273
if r.machineObj.Annotations[awsInternetGatewayIDAnnotation] != "" {
271274
if err := deleteAwsInternetGateway(c, r.machineObj.Annotations[awsInternetGatewayIDAnnotation], r.machineObj.Annotations[awsVPCIDAnnotation]); err != nil {
@@ -283,7 +286,7 @@ func (r *MachineReconciler) deleteAwsSubnet(c *ec2.EC2, subnetId string) error {
283286
return err
284287
}
285288

286-
r.log.Info("subnet successfully deleted")
289+
r.Log.Info("subnet successfully deleted")
287290
return nil
288291
}
289292

@@ -302,6 +305,7 @@ func getVPC(c *ec2.EC2, vpcId *string) (*ec2.Vpc, error) {
302305
}
303306
return nil, fmt.Errorf("no vpc found with id %s", *vpcId)
304307
}
308+
305309
func (r *MachineReconciler) createAwsVpc(c *ec2.EC2) error {
306310
var vpc *ec2.Vpc
307311
var err error
@@ -356,6 +360,7 @@ func (r *MachineReconciler) createAwsVpc(c *ec2.EC2) error {
356360
klog.Infof("aws vpc created with id %s", *vpc.VpcId)
357361
return nil
358362
}
363+
359364
func (r *MachineReconciler) deleteAwsVpc(c *ec2.EC2, vpcID string) error {
360365
if r.machineObj.Annotations[awsVPCIDAnnotation] == "" {
361366
return nil
@@ -410,12 +415,12 @@ func deleteSecurityGroup(c *ec2.EC2, vpcId string) error {
410415
return nil
411416
}
412417

413-
func (r *MachineReconciler) createAWSEnvironment(ctx context.Context) error {
418+
func (r *MachineReconciler) createAWSEnvironment() error {
414419
if r.machineObj.Annotations[awsVPCIDAnnotation] != "" && r.machineObj.Annotations[awsSubnetIDAnnotation] != "" && r.machineObj.Annotations[awsInternetGatewayIDAnnotation] != "" {
415420
return nil
416421
}
417422

418-
c, err := r.awsEC2Client(ctx)
423+
c, err := r.awsEC2Client()
419424
if err != nil {
420425
return err
421426
}

0 commit comments

Comments
 (0)