Skip to content

Commit

Permalink
Update internal/controller/deployment_controller.go
Browse files Browse the repository at this point in the history
Co-authored-by: Kyle Squizzato <[email protected]>
Signed-off-by: Kyle <[email protected]>
  • Loading branch information
kylewuolle and squizzi committed Aug 27, 2024
1 parent e8dc53e commit 2eadd5c
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ COPY internal/ internal/
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -ldflags="${LD_FLAGS}" -a -o manager cmd/main.go
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -gcflags=all="-N -l" -ldflags="${LD_FLAGS}" -a -o manager cmd/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ lint-chart-%:
package-chart-%: $(CHARTS_PACKAGE_DIR) lint-chart-%
$(HELM) package --destination $(CHARTS_PACKAGE_DIR) $(TEMPLATES_DIR)/$*

LD_FLAGS?= -s -w
LD_FLAGS += -X github.com/Mirantis/hmc/internal/build.Version=$(VERSION)
LD_FLAGS = -X github.com/Mirantis/hmc/internal/build.Version=$(VERSION)
LD_FLAGS += -X github.com/Mirantis/hmc/internal/telemetry.segmentToken=$(SEGMENT_TOKEN)

.PHONY: build
Expand Down
4 changes: 2 additions & 2 deletions config/dev/awscredentials.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: v1
data:
credentials: Cg==
kind: Secret
credentials: ""
kind: Secret
metadata:
labels:
cluster.x-k8s.io/provider: infrastructure-aws
Expand Down
8 changes: 4 additions & 4 deletions config/dev/deployment.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
apiVersion: hmc.mirantis.com/v1alpha1
kind: Deployment
metadata:
name: aws-dev
name: tbone-aws-dev
spec:
template: aws-standalone-cp
config:
region: us-east-2
region: us-west-1
publicIP: true
controlPlaneNumber: 1
workersNumber: 1
controlPlane:
amiID: ami-02f3416038bdb17fb
amiID: ami-0e99d1e59ff320ab2
instanceType: t3.small
worker:
amiID: ami-02f3416038bdb17fb
amiID: ami-0e99d1e59ff320ab2
instanceType: t3.small
76 changes: 39 additions & 37 deletions internal/controller/deployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package controller

import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
Expand All @@ -31,6 +30,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -89,12 +89,7 @@ func (r *DeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return r.Update(ctx, l, deployment)
}

func (r *DeploymentReconciler) setStatusFromClusterStatus(ctx context.Context, l logr.Logger, deployment *hmc.Deployment) (bool, error) {
dc, err := dynamic.NewForConfig(r.Config)
if err != nil {
return true, fmt.Errorf("failed to create client configuration: %v", err)
}

func (r *DeploymentReconciler) setStatusFromClusterStatus(ctx context.Context, dc *dynamic.DynamicClient, l logr.Logger, deployment *hmc.Deployment) (bool, error) {
resourceId := schema.GroupVersionResource{
Group: "cluster.x-k8s.io",
Version: "v1beta1",
Expand All @@ -108,45 +103,52 @@ func (r *DeploymentReconciler) setStatusFromClusterStatus(ctx context.Context, l
}

if err != nil {
return true, fmt.Errorf("failed to get cluster information for deployment: %s: %w", deployment.Name, err)
return true, fmt.Errorf("failed to get cluster information for deployment %s in namespace: %s: %w",
deployment.Namespace, deployment.Name, err)
}
conditions, found, err := unstructured.NestedSlice(list.Object, "status", "conditions")
if err != nil {
return true, fmt.Errorf("failed to get cluster information for deployment %s in namespace: %s: %w",
deployment.Namespace, deployment.Name, err)
}
if !found {
return true, fmt.Errorf("failed to get cluster information for deployment %s in namespace: %s: status.conditions not found",
deployment.Namespace, deployment.Name)
}

requeue := true
if statusField, ok := list.Object["status"]; ok {
if statusMap, ok := statusField.(map[string]interface{}); ok {
if conditionsField, ok := statusMap["conditions"]; ok {
bytes, err := json.Marshal(conditionsField)
if err != nil {
return true, fmt.Errorf("failed to get deserialize cluster information for deployment: %s : %v",
deployment.Name, err)
}
var conditions []metav1.Condition
err = json.Unmarshal(bytes, &conditions)
if err != nil {
return true, fmt.Errorf("failed to get serialize cluster information for deployment: %s : %v",
deployment.Name, err)
}
allConditionsComplete := true
for _, condition := range conditions {
conditionMap, ok := condition.(map[string]interface{})
if !ok {
return true, fmt.Errorf("failed to cast condition to map[string]interface{} for deployment: %s in namespace: %s: %w",
deployment.Namespace, deployment.Name, err)
}

allConditionsComplete := true
for _, condition := range conditions {
if condition.Status != "True" {
allConditionsComplete = false
}
var metaCondition metav1.Condition
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(conditionMap, &metaCondition); err != nil {
return true, fmt.Errorf("failed to convert unstructured conditions to metav1.Condition for deployment %s in namespace: %s: %w",
deployment.Namespace, deployment.Name, err)
}

if condition.Reason == "" && condition.Status == "True" {
condition.Reason = "Succeeded"
}
apimeta.SetStatusCondition(deployment.GetConditions(), condition)
}
requeue = !allConditionsComplete
}
if metaCondition.Status != "True" {
allConditionsComplete = false
}

if metaCondition.Reason == "" && metaCondition.Status == "True" {
metaCondition.Reason = "Succeeded"
}
apimeta.SetStatusCondition(deployment.GetConditions(), metaCondition)
}

return requeue, nil
return !allConditionsComplete, nil
}

func (r *DeploymentReconciler) Update(ctx context.Context, l logr.Logger, deployment *hmc.Deployment) (result ctrl.Result, err error) {
dc, err := dynamic.NewForConfig(r.Config)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to create client configuration: %v", err)
}

finalizersUpdated := controllerutil.AddFinalizer(deployment, hmc.DeploymentFinalizer)
if finalizersUpdated {
if err := r.Client.Update(ctx, deployment); err != nil {
Expand Down Expand Up @@ -284,7 +286,7 @@ func (r *DeploymentReconciler) Update(ctx context.Context, l logr.Logger, deploy
})
}

requeue, err := r.setStatusFromClusterStatus(ctx, l, deployment)
requeue, err := r.setStatusFromClusterStatus(ctx, dc, l, deployment)
if err != nil {
if requeue {
return ctrl.Result{RequeueAfter: 10 * time.Second}, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ spec:
{{- end }}
securityContext:
fsGroup: 1000
runAsNonRoot: true
runAsNonRoot: false
seccompProfile:
type: RuntimeDefault
serviceAccountName: {{ include "cluster-api-provider-aws.fullname" . }}-controller-manager
Expand Down
2 changes: 1 addition & 1 deletion templates/cluster-api/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ spec:
name: cert
readOnly: true
securityContext:
runAsNonRoot: true
runAsNonRoot: false
seccompProfile:
type: RuntimeDefault
serviceAccountName: {{ include "cluster-api.fullname" . }}-manager
Expand Down
4 changes: 2 additions & 2 deletions templates/hmc/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ spec:
readOnly: true
{{- end }}
securityContext:
runAsNonRoot: true
runAsNonRoot: false
serviceAccountName: {{ include "hmc.fullname" . }}-controller-manager
terminationGracePeriodSeconds: 10
terminationGracePeriodSeconds: 6000
{{- if .Values.admissionWebhook.enabled }}
volumes:
- name: cert
Expand Down
2 changes: 1 addition & 1 deletion templates/k0smotron/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@ spec:
drop:
- ALL
securityContext:
runAsNonRoot: true
runAsNonRoot: false
serviceAccountName: {{ include "k0smotron.fullname" . }}-controller-manager
terminationGracePeriodSeconds: 10

0 comments on commit 2eadd5c

Please sign in to comment.