From faa6e74dc359165411b39ba371baa103cfa5b938 Mon Sep 17 00:00:00 2001 From: Christoph Mewes Date: Wed, 13 Mar 2024 21:55:56 +0100 Subject: [PATCH] use existing logic to determine when a deployment has rolled out --- .../application_catalog.go | 8 +-- pkg/install/util/deploy.go | 64 ------------------- pkg/install/util/deployment.go | 51 +++++++++++++++ 3 files changed, 55 insertions(+), 68 deletions(-) delete mode 100644 pkg/install/util/deploy.go create mode 100644 pkg/install/util/deployment.go diff --git a/pkg/ee/default-application-catalog/application_catalog.go b/pkg/ee/default-application-catalog/application_catalog.go index 94fc3569f7..a86cdf59bc 100644 --- a/pkg/ee/default-application-catalog/application_catalog.go +++ b/pkg/ee/default-application-catalog/application_catalog.go @@ -58,15 +58,15 @@ func DeployDefaultApplicationCatalog(ctx context.Context, logger *logrus.Entry, return fmt.Errorf("failed to fetch ApplicationDefinitions: %w", err) } - sublogger.Infof("Waiting for webhook %q with version %q to become ready", common.WebhookDeploymentName, opt.Versions.Kubermatic) - operatorDeploySelector := &appsv1.Deployment{ + sublogger.Info("Waiting for KKP webhook to become ready…") + webhook := &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: common.WebhookDeploymentName, Namespace: resources.KubermaticNamespace, }, } - if err := util.WaitForUpdatedDeployment(ctx, operatorDeploySelector, opt.Versions.Kubermatic, kubeClient, 5*time.Minute); err != nil { - return fmt.Errorf("failed waiting for webhook to come ready %w", err) + if err := util.WaitForDeploymentRollout(ctx, kubeClient, webhook, opt.Versions.Kubermatic, 5*time.Minute); err != nil { + return fmt.Errorf("failed waiting for webhook: %w", err) } creators := []kkpreconciling.NamedApplicationDefinitionReconcilerFactory{} diff --git a/pkg/install/util/deploy.go b/pkg/install/util/deploy.go deleted file mode 100644 index 67a9157ae8..0000000000 --- a/pkg/install/util/deploy.go +++ /dev/null @@ -1,64 +0,0 @@ -/* -Copyright 2024 The Kubermatic Kubernetes Platform 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 util - -import ( - "context" - "time" - - "k8c.io/kubermatic/v2/pkg/resources" - - appsv1 "k8s.io/api/apps/v1" - apierrors "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/types" - "k8s.io/apimachinery/pkg/util/wait" - ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client" -) - -// DeployMatchesVersion returns true if the version label of the deployment matches the supplied version. -func DeployMatchesVersion(deploy *appsv1.Deployment, version string) bool { - return deploy.Labels[resources.VersionLabel] == version -} - -// DeployReplicasReady returns true if all replicas of a deployment are ready. -func DeployReplicasReady(deploy *appsv1.Deployment) bool { - return deploy.Status.ReadyReplicas == *deploy.Spec.Replicas -} - -// WaitForUpdatedDeployment queries k8s until a deployment with the supplied version exists or until the timeout is reached. -func WaitForUpdatedDeployment(ctx context.Context, deployToWatch *appsv1.Deployment, version string, kubeClient ctrlruntimeclient.Client, timeout time.Duration) error { - return wait.PollUntilContextTimeout(ctx, 1*time.Second, timeout, true, func(ctx context.Context) (bool, error) { - res := &appsv1.Deployment{} - nsn := types.NamespacedName{Namespace: deployToWatch.Namespace, Name: deployToWatch.Name} - - if err := kubeClient.Get(ctx, nsn, res); err != nil { - if apierrors.IsNotFound(err) { - return false, nil - } - - return false, err - } - - versionCheck := DeployMatchesVersion(res, version) - // we can exit early if the Version doesn't match, no need to check for replicas - if !versionCheck { - return false, nil - } - - return DeployReplicasReady(res), nil - }) -} diff --git a/pkg/install/util/deployment.go b/pkg/install/util/deployment.go new file mode 100644 index 0000000000..b671eb5426 --- /dev/null +++ b/pkg/install/util/deployment.go @@ -0,0 +1,51 @@ +/* +Copyright 2024 The Kubermatic Kubernetes Platform 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 util + +import ( + "context" + "time" + + "k8c.io/kubermatic/v2/pkg/kubernetes" + "k8c.io/kubermatic/v2/pkg/resources" + + appsv1 "k8s.io/api/apps/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/util/wait" + ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client" +) + +// WaitForDeploymentRollout queries k8s until a deployment with the supplied version exists and +// has been rolled out, or until the timeout is reached. +func WaitForDeploymentRollout(ctx context.Context, client ctrlruntimeclient.Client, deployment *appsv1.Deployment, version string, timeout time.Duration) error { + return wait.PollUntilContextTimeout(ctx, 1*time.Second, timeout, true, func(ctx context.Context) (bool, error) { + dep := &appsv1.Deployment{} + if err := client.Get(ctx, ctrlruntimeclient.ObjectKeyFromObject(deployment), dep); err != nil { + if apierrors.IsNotFound(err) { + return false, nil + } + + return false, err + } + + if dep.Labels[resources.VersionLabel] != version { + return false, nil + } + + return kubernetes.IsDeploymentRolloutComplete(dep, 0) + }) +}