-
Notifications
You must be signed in to change notification settings - Fork 7
feat: Add k8s version logic for external cloud-provider flag #1134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,97 @@ | ||||||
// Copyright 2023 Nutanix. All rights reserved. | ||||||
// SPDX-License-Identifier: Apache-2.0 | ||||||
package externalcloudprovider | ||||||
|
||||||
import ( | ||||||
"context" | ||||||
"fmt" | ||||||
|
||||||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||||||
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" | ||||||
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1" | ||||||
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" | ||||||
ctrl "sigs.k8s.io/controller-runtime" | ||||||
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" | ||||||
|
||||||
"github.com/blang/semver/v4" | ||||||
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation" | ||||||
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches" | ||||||
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches/selectors" | ||||||
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/variables" | ||||||
) | ||||||
|
||||||
var ( | ||||||
versionGreatOrEqualTo133Range = semver.MustParseRange(">=1.33.0-0") | ||||||
) | ||||||
|
||||||
type externalCloudProviderPatchHandler struct{} | ||||||
|
||||||
func NewControlPlanePatch() *externalCloudProviderPatchHandler { | ||||||
return &externalCloudProviderPatchHandler{} | ||||||
} | ||||||
|
||||||
func (h *externalCloudProviderPatchHandler) Mutate( | ||||||
ctx context.Context, | ||||||
obj *unstructured.Unstructured, | ||||||
vars map[string]apiextensionsv1.JSON, | ||||||
holderRef runtimehooksv1.HolderReference, | ||||||
_ ctrlclient.ObjectKey, | ||||||
_ mutation.ClusterGetter, | ||||||
) error { | ||||||
log := ctrl.LoggerFrom(ctx).WithValues( | ||||||
"holderRef", holderRef, | ||||||
) | ||||||
|
||||||
cpVersion, err := variables.Get[string](vars, runtimehooksv1.BuiltinsName, "controlPlane", "version") | ||||||
if err != nil { | ||||||
// This builtin variable is guaranteed to be provided for control plane component patch requests so if it is not | ||||||
// found then we can safely skip this patch for this request item. | ||||||
if variables.IsFieldNotFoundError(err) { | ||||||
log.V(5). | ||||||
WithValues("variables", vars). | ||||||
Info( | ||||||
"skipping external cloud-provider flag to control plane because CP Kubernetes version is not found", | ||||||
) | ||||||
return nil | ||||||
} | ||||||
|
||||||
// This is a fatal error, we can't proceed without the control plane version. | ||||||
log.WithValues("variables", vars).Error(err, "failed to get control plane Kubernetes version from builtin variable") | ||||||
return fmt.Errorf("failed to get control plane Kubernetes version from builtin variable: %w", err) | ||||||
} | ||||||
|
||||||
cpK8sVersion, err := semver.ParseTolerant(cpVersion) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: the codebase mostly does not use "k8sVersion"
Suggested change
|
||||||
if err != nil { | ||||||
log.WithValues( | ||||||
"kubernetesVersion", | ||||||
cpVersion, | ||||||
).Error(err, "failed to parse control plane Kubernetes version") | ||||||
return fmt.Errorf("failed to parse control plane Kubernetes version: %w", err) | ||||||
} | ||||||
|
||||||
if versionGreatOrEqualTo133Range(cpK8sVersion) { | ||||||
log.V(5).Info( | ||||||
"skipping external cloud-provider flag to control plane kubeadm config template because Kubernetes < 1.33.0", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
) | ||||||
return nil | ||||||
} | ||||||
|
||||||
if err := patches.MutateIfApplicable( | ||||||
obj, vars, &holderRef, selectors.ControlPlane(), log, | ||||||
func(obj *controlplanev1.KubeadmControlPlaneTemplate) error { | ||||||
if obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration == nil { | ||||||
obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration = &bootstrapv1.ClusterConfiguration{} | ||||||
} | ||||||
if obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration.APIServer.ExtraArgs == nil { | ||||||
obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration.APIServer.ExtraArgs = make(map[string]string, 1) | ||||||
} | ||||||
obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration.APIServer.ExtraArgs["cloud-provider"] = "external" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it still possible for this value to be anything else, I think it can even be set to ""? Should we only set it if the key is missing? |
||||||
|
||||||
return nil | ||||||
}); err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
return nil | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really understand the intent of these changes, please reply here.