diff --git a/Dockerfile b/Dockerfile index 6b75560d2..1ba5bb6e2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 -gcflags=all="-N -l" -ldflags="${LD_FLAGS}" -a -o manager cmd/main.go +RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -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 diff --git a/Makefile b/Makefile index ae68421bd..fd18a36ca 100644 --- a/Makefile +++ b/Makefile @@ -141,7 +141,8 @@ lint-chart-%: package-chart-%: $(CHARTS_PACKAGE_DIR) lint-chart-% $(HELM) package --destination $(CHARTS_PACKAGE_DIR) $(TEMPLATES_DIR)/$* -LD_FLAGS = -X github.com/Mirantis/hmc/internal/build.Version=$(VERSION) +LD_FLAGS?= -s -w +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 diff --git a/api/v1alpha1/deployment_types.go b/api/v1alpha1/deployment_types.go index 099bb0b4f..93a69fe14 100644 --- a/api/v1alpha1/deployment_types.go +++ b/api/v1alpha1/deployment_types.go @@ -24,6 +24,7 @@ import ( const ( DeploymentFinalizer = "hmc.mirantis.com/deployment" + FluxHelmChartNameKey = "helm.toolkit.fluxcd.io/name" HMCManagedLabelKey = "hmc.mirantis.com/managed" HMCManagedLabelValue = "true" ) diff --git a/config/dev/deployment.yaml b/config/dev/deployment.yaml index da64faeee..58ba18e32 100644 --- a/config/dev/deployment.yaml +++ b/config/dev/deployment.yaml @@ -1,17 +1,17 @@ apiVersion: hmc.mirantis.com/v1alpha1 kind: Deployment metadata: - name: tbone-aws-dev + name: aws-dev spec: template: aws-standalone-cp config: - region: us-west-1 + region: us-east-2 publicIP: true controlPlaneNumber: 1 workersNumber: 1 controlPlane: - amiID: ami-0e99d1e59ff320ab2 + amiID: ami-02f3416038bdb17fb instanceType: t3.small worker: - amiID: ami-0e99d1e59ff320ab2 + amiID: ami-02f3416038bdb17fb instanceType: t3.small diff --git a/internal/controller/deployment_controller.go b/internal/controller/deployment_controller.go index 31972f4e3..f16e0fef1 100644 --- a/internal/controller/deployment_controller.go +++ b/internal/controller/deployment_controller.go @@ -20,6 +20,8 @@ import ( "fmt" "time" + "k8s.io/apimachinery/pkg/labels" + hcv2 "github.com/fluxcd/helm-controller/api/v2" fluxmeta "github.com/fluxcd/pkg/apis/meta" fluxconditions "github.com/fluxcd/pkg/runtime/conditions" @@ -96,9 +98,12 @@ func (r *DeploymentReconciler) setStatusFromClusterStatus(ctx context.Context, d Resource: "clusters", } - list, err := dc.Resource(resourceId).Namespace(deployment.Namespace).Get(ctx, deployment.Name, metav1.GetOptions{}) - if apierrors.IsNotFound(err) { - l.Info("Cluster not found, ignoring since object must be deleted or not yet created") + list, err := dc.Resource(resourceId).Namespace(deployment.Namespace).List(ctx, metav1.ListOptions{ + LabelSelector: labels.SelectorFromSet(map[string]string{hmc.FluxHelmChartNameKey: deployment.Name}).String(), + }) + + if apierrors.IsNotFound(err) || len(list.Items) == 0 { + l.Info("Clusters not found, ignoring since object must be deleted or not yet created") return true, nil } @@ -106,7 +111,7 @@ func (r *DeploymentReconciler) setStatusFromClusterStatus(ctx context.Context, d 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") + conditions, found, err := unstructured.NestedSlice(list.Items[0].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) diff --git a/templates/hmc/templates/deployment.yaml b/templates/hmc/templates/deployment.yaml index 81ba36d64..028835a1a 100644 --- a/templates/hmc/templates/deployment.yaml +++ b/templates/hmc/templates/deployment.yaml @@ -70,9 +70,9 @@ spec: readOnly: true {{- end }} securityContext: - runAsNonRoot: false + runAsNonRoot: true serviceAccountName: {{ include "hmc.fullname" . }}-controller-manager - terminationGracePeriodSeconds: 6000 + terminationGracePeriodSeconds: 10 {{- if .Values.admissionWebhook.enabled }} volumes: - name: cert