diff --git a/Gopkg.lock b/Gopkg.lock index aad8f08719..4c7ee1cc9d 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -459,7 +459,7 @@ [[projects]] branch = "openshift-4.2-cluster-api-0.1.0" - digest = "1:54dfec845e2fb5631fa1fc556a677a473a7efe663df4a4808621dca5e0358b23" + digest = "1:e4cc8b4a309af144c12f7e010efe4de2a3068cbb141a856bc2e662608683d3f3" name = "github.com/openshift/cluster-api" packages = [ "pkg/apis", @@ -479,7 +479,7 @@ "pkg/util", ] pruneopts = "T" - revision = "72ebbaa0456c052d886259dc26420644178a0c5f" + revision = "f8de78af80fcecf1fd69e35005b591dc9e1df61a" [[projects]] branch = "master" diff --git a/pkg/actuators/machine/actuator.go b/pkg/actuators/machine/actuator.go index a6f877dc97..61143ee004 100644 --- a/pkg/actuators/machine/actuator.go +++ b/pkg/actuators/machine/actuator.go @@ -42,6 +42,8 @@ import ( awsclient "sigs.k8s.io/cluster-api-provider-aws/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client" + + machinecontroller "github.com/openshift/cluster-api/pkg/controller/machine" ) const ( @@ -122,9 +124,58 @@ func (a *Actuator) Create(context context.Context, cluster *clusterv1.Cluster, m if err != nil { return fmt.Errorf("%s: failed to update machine object with providerID: %v", machine.Name, err) } + + updatedMachine, err = a.setMachineCloudProviderSpecifics(updatedMachine, instance) + if err != nil { + return fmt.Errorf("%s: failed to set machine cloud provider specifics: %v", machine.Name, err) + } + return a.updateStatus(updatedMachine, instance) } +// updateProviderID adds providerID in the machine spec +func (a *Actuator) setMachineCloudProviderSpecifics(machine *machinev1.Machine, instance *ec2.Instance) (*machinev1.Machine, error) { + if instance == nil { + return machine, nil + } + machineCopy := machine.DeepCopy() + + if machineCopy.Labels == nil { + machineCopy.Labels = make(map[string]string) + } + + if machineCopy.Annotations == nil { + machineCopy.Annotations = make(map[string]string) + } + + // Reaching to machine provider config since the region is not directly + // providing by *ec2.Instance object + machineProviderConfig, err := providerConfigFromMachine(machine, a.codec) + if err != nil { + return nil, fmt.Errorf("error decoding MachineProviderConfig: %v", err) + } + + machineCopy.Labels[machinecontroller.MachineRegionLabelName] = machineProviderConfig.Placement.Region + + if instance.Placement != nil { + machineCopy.Labels[machinecontroller.MachineAZLabelName] = aws.StringValue(instance.Placement.AvailabilityZone) + } + + if instance.InstanceType != nil { + machineCopy.Labels[machinecontroller.MachineInstanceTypeLabelName] = aws.StringValue(instance.InstanceType) + } + + if instance.State != nil && instance.State.Name != nil { + machineCopy.Annotations[machinecontroller.MachineInstanceStateAnnotationName] = aws.StringValue(instance.State.Name) + } + + if err := a.client.Update(context.Background(), machineCopy); err != nil { + return nil, fmt.Errorf("%s: error updating machine spec: %v", machine.Name, err) + } + + return machineCopy, nil +} + // updateProviderID adds providerID in the machine spec func (a *Actuator) updateProviderID(machine *machinev1.Machine, instance *ec2.Instance) (*machinev1.Machine, error) { existingProviderID := machine.Spec.ProviderID @@ -325,10 +376,19 @@ func (a *Actuator) DeleteMachine(cluster *clusterv1.Cluster, machine *machinev1. return nil } - err = terminateInstances(client, instances) + terminatingInstances, err := terminateInstances(client, instances) if err != nil { return a.handleMachineError(machine, apierrors.DeleteMachine(err.Error()), noEventAction) } + + if len(terminatingInstances) == 1 { + if terminatingInstances[0] != nil && terminatingInstances[0].CurrentState != nil && terminatingInstances[0].CurrentState.Name != nil { + machineCopy := machine.DeepCopy() + machineCopy.Annotations[machinecontroller.MachineInstanceStateAnnotationName] = aws.StringValue(terminatingInstances[0].CurrentState.Name) + a.client.Update(context.Background(), machineCopy) + } + } + a.eventRecorder.Eventf(machine, corev1.EventTypeNormal, "Deleted", "Deleted machine %v", machine.Name) return nil @@ -404,6 +464,11 @@ func (a *Actuator) Update(context context.Context, cluster *clusterv1.Cluster, m a.eventRecorder.Eventf(machine, corev1.EventTypeNormal, "Updated", "Updated machine %v", machine.Name) + machine, err = a.setMachineCloudProviderSpecifics(machine, newestInstance) + if err != nil { + return fmt.Errorf("%s: failed to set machine cloud provider specifics: %v", machine.Name, err) + } + // We do not support making changes to pre-existing instances, just update status. return a.updateStatus(machine, newestInstance) } diff --git a/pkg/actuators/machine/instances.go b/pkg/actuators/machine/instances.go index a8506a33a6..c377a72841 100644 --- a/pkg/actuators/machine/instances.go +++ b/pkg/actuators/machine/instances.go @@ -45,7 +45,8 @@ func removeStoppedMachine(machine *machinev1.Machine, client awsclient.Client) e return nil } - return terminateInstances(client, instances) + _, err = terminateInstances(client, instances) + return err } func buildEC2Filters(inputFilters []providerconfigv1.Filter) []*ec2.Filter { diff --git a/pkg/actuators/machine/utils.go b/pkg/actuators/machine/utils.go index dd07a30819..98bebcc2e1 100644 --- a/pkg/actuators/machine/utils.go +++ b/pkg/actuators/machine/utils.go @@ -144,7 +144,7 @@ func getVolume(client awsclient.Client, volumeID string) (*ec2.Volume, error) { } // terminateInstances terminates all provided instances with a single EC2 request. -func terminateInstances(client awsclient.Client, instances []*ec2.Instance) error { +func terminateInstances(client awsclient.Client, instances []*ec2.Instance) ([]*ec2.InstanceStateChange, error) { instanceIDs := []*string{} // Cleanup all older instances: for _, instance := range instances { @@ -158,12 +158,17 @@ func terminateInstances(client awsclient.Client, instances []*ec2.Instance) erro terminateInstancesRequest := &ec2.TerminateInstancesInput{ InstanceIds: instanceIDs, } - _, err := client.TerminateInstances(terminateInstancesRequest) + output, err := client.TerminateInstances(terminateInstancesRequest) if err != nil { glog.Errorf("Error terminating instances: %v", err) - return fmt.Errorf("error terminating instances: %v", err) + return nil, fmt.Errorf("error terminating instances: %v", err) } - return nil + + if output == nil { + return nil, nil + } + + return output.TerminatingInstances, nil } // providerConfigFromMachine gets the machine provider config MachineSetSpec from the diff --git a/vendor/github.com/openshift/cluster-api/config/crds/machine_v1beta1_machine.yaml b/vendor/github.com/openshift/cluster-api/config/crds/machine_v1beta1_machine.yaml index 35dd1f4af4..b5cc2a4714 100644 --- a/vendor/github.com/openshift/cluster-api/config/crds/machine_v1beta1_machine.yaml +++ b/vendor/github.com/openshift/cluster-api/config/crds/machine_v1beta1_machine.yaml @@ -6,6 +6,37 @@ metadata: controller-tools.k8s.io: "1.0" name: machines.machine.openshift.io spec: + additionalPrinterColumns: + - JSONPath: .metadata.annotations['machine\.openshift\.io/instance-state'] + description: State of instance + name: State + type: string + - JSONPath: .metadata.labels['machine\.openshift\.io/instance-type'] + description: Type of instance + name: Type + type: string + - JSONPath: .metadata.labels['machine\.openshift\.io/region'] + description: Region associated with machine + name: Region + type: string + - JSONPath: .metadata.labels['machine\.openshift\.io/zone'] + description: Zone associated with machine + name: Zone + type: string + - JSONPath: .metadata.creationTimestamp + description: Machine age + name: Age + type: date + - JSONPath: .status.nodeRef.name + description: Node associated with machine + name: Node + priority: 1 + type: string + - JSONPath: .spec.providerID + description: Provider ID of machine created in cloud provider + name: ProviderID + priority: 1 + type: string group: machine.openshift.io names: kind: Machine diff --git a/vendor/github.com/openshift/cluster-api/config/crds/machine_v1beta1_machineset.yaml b/vendor/github.com/openshift/cluster-api/config/crds/machine_v1beta1_machineset.yaml index 4e88e2b694..0cbdf333b9 100644 --- a/vendor/github.com/openshift/cluster-api/config/crds/machine_v1beta1_machineset.yaml +++ b/vendor/github.com/openshift/cluster-api/config/crds/machine_v1beta1_machineset.yaml @@ -6,6 +6,27 @@ metadata: controller-tools.k8s.io: "1.0" name: machinesets.machine.openshift.io spec: + additionalPrinterColumns: + - JSONPath: .spec.replicas + description: Desired Replicas + name: Desired + type: integer + - JSONPath: .status.replicas + description: Current Replicas + name: Current + type: integer + - JSONPath: .status.readyReplicas + description: Ready Replicas + name: Ready + type: integer + - JSONPath: .status.availableReplicas + description: Observed number of available replicas + name: Available + type: string + - JSONPath: .metadata.creationTimestamp + description: Machineset age + name: Age + type: date group: machine.openshift.io names: kind: MachineSet diff --git a/vendor/github.com/openshift/cluster-api/pkg/apis/machine/v1beta1/machine_types.go b/vendor/github.com/openshift/cluster-api/pkg/apis/machine/v1beta1/machine_types.go index 62ec393376..0bbc48f5a7 100644 --- a/vendor/github.com/openshift/cluster-api/pkg/apis/machine/v1beta1/machine_types.go +++ b/vendor/github.com/openshift/cluster-api/pkg/apis/machine/v1beta1/machine_types.go @@ -46,6 +46,13 @@ const ( // Machine is the Schema for the machines API // +k8s:openapi-gen=true // +kubebuilder:subresource:status +// +kubebuilder:printcolumn:name="State",type="string",JSONPath=".metadata.annotations['machine\.openshift\.io/instance-state']",description="State of instance" +// +kubebuilder:printcolumn:name="Type",type="string",JSONPath=".metadata.labels['machine\.openshift\.io/instance-type']",description="Type of instance" +// +kubebuilder:printcolumn:name="Region",type="string",JSONPath=".metadata.labels['machine\.openshift\.io/region']",description="Region associated with machine" +// +kubebuilder:printcolumn:name="Zone",type="string",JSONPath=".metadata.labels['machine\.openshift\.io/zone']",description="Zone associated with machine" +// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Machine age" +// +kubebuilder:printcolumn:name="Node",type="string",JSONPath=".status.nodeRef.name",description="Node associated with machine",priority=1 +// +kubebuilder:printcolumn:name="ProviderID",type="string",JSONPath=".spec.providerID",description="Provider ID of machine created in cloud provider",priority=1 type Machine struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` diff --git a/vendor/github.com/openshift/cluster-api/pkg/apis/machine/v1beta1/machineset_types.go b/vendor/github.com/openshift/cluster-api/pkg/apis/machine/v1beta1/machineset_types.go index b4edb7a303..413f598fe0 100644 --- a/vendor/github.com/openshift/cluster-api/pkg/apis/machine/v1beta1/machineset_types.go +++ b/vendor/github.com/openshift/cluster-api/pkg/apis/machine/v1beta1/machineset_types.go @@ -35,6 +35,11 @@ import ( // +k8s:openapi-gen=true // +kubebuilder:subresource:status // +kubebuilder:subresource:scale:specpath=.spec.replicas,statuspath=.status.replicas,selectorpath=.status.labelSelector +// +kubebuilder:printcolumn:name="Desired",type="integer",JSONPath=".spec.replicas",description="Desired Replicas" +// +kubebuilder:printcolumn:name="Current",type="integer",JSONPath=".status.replicas",description="Current Replicas" +// +kubebuilder:printcolumn:name="Ready",type="integer",JSONPath=".status.readyReplicas",description="Ready Replicas" +// +kubebuilder:printcolumn:name="Available",type="string",JSONPath=".status.availableReplicas",description="Observed number of available replicas" +// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Machineset age" type MachineSet struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` diff --git a/vendor/github.com/openshift/cluster-api/pkg/controller/machine/controller.go b/vendor/github.com/openshift/cluster-api/pkg/controller/machine/controller.go index f2f1711997..3d5f1e4122 100644 --- a/vendor/github.com/openshift/cluster-api/pkg/controller/machine/controller.go +++ b/vendor/github.com/openshift/cluster-api/pkg/controller/machine/controller.go @@ -49,6 +49,18 @@ const ( // ExcludeNodeDrainingAnnotation annotation explicitly skips node draining if set ExcludeNodeDrainingAnnotation = "machine.openshift.io/exclude-node-draining" + + // MachineRegionLabelName as annotation name for a machine region + MachineRegionLabelName = "machine.openshift.io/region" + + // MachineAZLabelName as annotation name for a machine AZ + MachineAZLabelName = "machine.openshift.io/zone" + + // MachineInstanceStateAnnotationName as annotation name for a machine instance state + MachineInstanceStateAnnotationName = "machine.openshift.io/instance-state" + + // MachineInstanceTypeLabelName as annotation name for a machine instance type + MachineInstanceTypeLabelName = "machine.openshift.io/instance-type" ) var DefaultActuator Actuator diff --git a/vendor/github.com/openshift/cluster-api/pkg/controller/machinedeployment/controller.go b/vendor/github.com/openshift/cluster-api/pkg/controller/machinedeployment/controller.go index e47bd3e88a..a77e7a6299 100644 --- a/vendor/github.com/openshift/cluster-api/pkg/controller/machinedeployment/controller.go +++ b/vendor/github.com/openshift/cluster-api/pkg/controller/machinedeployment/controller.go @@ -173,6 +173,13 @@ func (r *ReconcileMachineDeployment) Reconcile(request reconcile.Request) (recon // Error reading the object - requeue the request. return reconcile.Result{}, err } + + // Ignore deleted MachineDeployments, this can happen when foregroundDeletion + // is enabled + if d.DeletionTimestamp != nil { + return reconcile.Result{}, nil + } + result, err := r.reconcile(ctx, d) if err != nil { klog.Errorf("Failed to reconcile MachineDeployment %q: %v", request.NamespacedName, err) diff --git a/vendor/github.com/openshift/cluster-api/pkg/controller/machinedeployment/sync.go b/vendor/github.com/openshift/cluster-api/pkg/controller/machinedeployment/sync.go index 497167f8fd..40bca89603 100644 --- a/vendor/github.com/openshift/cluster-api/pkg/controller/machinedeployment/sync.go +++ b/vendor/github.com/openshift/cluster-api/pkg/controller/machinedeployment/sync.go @@ -31,6 +31,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" apirand "k8s.io/apimachinery/pkg/util/rand" + "k8s.io/apimachinery/pkg/util/sets" "k8s.io/client-go/util/retry" "k8s.io/klog" "sigs.k8s.io/controller-runtime/pkg/client" @@ -154,6 +155,11 @@ func (r *ReconcileMachineDeployment) getNewMachineSet(d *machinev1beta1.MachineD }, } + // Add foregroundDeletion finalizer to MachineSet if the MachineDeployment has it + if sets.NewString(d.Finalizers...).Has(metav1.FinalizerDeleteDependents) { + newMS.Finalizers = []string{metav1.FinalizerDeleteDependents} + } + allMSs := append(oldMSs, &newMS) newReplicasCount, err := dutil.NewMSNewReplicas(d, allMSs, &newMS) if err != nil { diff --git a/vendor/github.com/openshift/cluster-api/pkg/controller/machineset/controller.go b/vendor/github.com/openshift/cluster-api/pkg/controller/machineset/controller.go index db2678c5a2..17c44f763e 100644 --- a/vendor/github.com/openshift/cluster-api/pkg/controller/machineset/controller.go +++ b/vendor/github.com/openshift/cluster-api/pkg/controller/machineset/controller.go @@ -158,6 +158,12 @@ func (r *ReconcileMachineSet) Reconcile(request reconcile.Request) (reconcile.Re return reconcile.Result{}, err } + // Ignore deleted MachineSets, this can happen when foregroundDeletion + // is enabled + if machineSet.DeletionTimestamp != nil { + return reconcile.Result{}, nil + } + result, err := r.reconcile(ctx, machineSet) if err != nil { klog.Errorf("Failed to reconcile MachineSet %q: %v", request.NamespacedName, err)