This repository was archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add user configuration for all providers #4
Merged
Merged
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9f56b56
feat: Add user configuration for all providers
dlipovetsky 650c3cf
fixup! feat: Add user configuration for all providers
dlipovetsky 27072c6
fixup! feat: Add user configuration for all providers
dlipovetsky 9748994
fixup! feat: Add user configuration for all providers
dlipovetsky 1205a59
fixup! feat: Add user configuration for all providers
dlipovetsky a0cdd4b
fixup! feat: Add user configuration for all providers
dlipovetsky 64b7afc
fixup! feat: Add user configuration for all providers
dlipovetsky 37ec7c9
fixup! feat: Add user configuration for all providers
dlipovetsky 0968ea6
fixup! feat: Add user configuration for all providers
dlipovetsky f09a28f
fixup! feat: Add user configuration for all providers
dlipovetsky 5a73b71
fixup! feat: Add user configuration for all providers
dlipovetsky b8bcb0e
fixup! feat: Add user configuration for all providers
dlipovetsky dfd5cbe
fixup! fixup! feat: Add user configuration for all providers
dlipovetsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Copyright 2023 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package users |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright 2023 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package users | ||
|
||
import ( | ||
"context" | ||
|
||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/utils/ptr" | ||
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/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/v1alpha1" | ||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches" | ||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches/selectors" | ||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/variables" | ||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig" | ||
) | ||
|
||
const ( | ||
// VariableName is the external patch variable name. | ||
VariableName = "users" | ||
) | ||
|
||
type usersPatchHandler struct { | ||
variableName string | ||
variableFieldPath []string | ||
} | ||
|
||
func NewPatch() *usersPatchHandler { | ||
return newUsersPatchHandler( | ||
clusterconfig.MetaVariableName, | ||
VariableName) | ||
} | ||
|
||
func newUsersPatchHandler( | ||
variableName string, | ||
variableFieldPath ...string, | ||
) *usersPatchHandler { | ||
return &usersPatchHandler{ | ||
variableName: variableName, | ||
variableFieldPath: variableFieldPath, | ||
} | ||
} | ||
|
||
func (h *usersPatchHandler) Mutate( | ||
dlipovetsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ctx context.Context, | ||
obj *unstructured.Unstructured, | ||
vars map[string]apiextensionsv1.JSON, | ||
holderRef runtimehooksv1.HolderReference, | ||
_ ctrlclient.ObjectKey, | ||
) error { | ||
log := ctrl.LoggerFrom(ctx, "holderRef", holderRef) | ||
|
||
usersVariable, found, err := variables.Get[v1alpha1.Users]( | ||
vars, | ||
h.variableName, | ||
h.variableFieldPath..., | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
if !found { | ||
log.V(5).Info("users variable not defined") | ||
return nil | ||
} | ||
|
||
log = log.WithValues( | ||
"variableName", | ||
h.variableName, | ||
"variableFieldPath", | ||
h.variableFieldPath, | ||
"variableValue", | ||
usersVariable, | ||
) | ||
|
||
if err := patches.MutateIfApplicable( | ||
obj, vars, &holderRef, selectors.ControlPlane(), log, | ||
func(obj *controlplanev1.KubeadmControlPlaneTemplate) error { | ||
log.WithValues( | ||
"patchedObjectKind", obj.GetObjectKind().GroupVersionKind().String(), | ||
"patchedObjectName", ctrlclient.ObjectKeyFromObject(obj), | ||
).Info("setting users in control plane kubeadm config template") | ||
bootstrapUsers := []bootstrapv1.User{} | ||
for _, userFromVariable := range usersVariable { | ||
bootstrapUsers = append(bootstrapUsers, generateBootstrapUser(userFromVariable)) | ||
} | ||
obj.Spec.Template.Spec.KubeadmConfigSpec.Users = bootstrapUsers | ||
return nil | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
if err := patches.MutateIfApplicable( | ||
obj, vars, &holderRef, selectors.WorkersKubeadmConfigTemplateSelector(), log, | ||
func(obj *bootstrapv1.KubeadmConfigTemplate) error { | ||
log.WithValues( | ||
"patchedObjectKind", obj.GetObjectKind().GroupVersionKind().String(), | ||
"patchedObjectName", ctrlclient.ObjectKeyFromObject(obj), | ||
).Info("setting users in worker node kubeadm config template") | ||
bootstrapUsers := []bootstrapv1.User{} | ||
for _, userFromVariable := range usersVariable { | ||
bootstrapUsers = append(bootstrapUsers, generateBootstrapUser(userFromVariable)) | ||
} | ||
obj.Spec.Template.Spec.Users = bootstrapUsers | ||
return nil | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func generateBootstrapUser(userFromVariable v1alpha1.User) bootstrapv1.User { | ||
bootstrapUser := bootstrapv1.User{ | ||
Name: userFromVariable.Name, | ||
Passwd: ptr.To(userFromVariable.HashedPassword), | ||
SSHAuthorizedKeys: userFromVariable.SSHAuthorizedKeys, | ||
Sudo: userFromVariable.Sudo, | ||
} | ||
|
||
// LockPassword is not part of our API, because we can derive its value | ||
faiq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// for the use cases our API supports. | ||
// | ||
// We do not support these edge cases: | ||
// (a) Hashed password is defined, password authentication is not enabled. | ||
// (b) Hashed password is not defined, password authentication is enabled. | ||
// | ||
// We disable password authentication by default. | ||
bootstrapUser.LockPassword = ptr.To(true) | ||
if userFromVariable.HashedPassword != "" { | ||
// We enable password authentication only if a hashed password is defined. | ||
bootstrapUser.LockPassword = ptr.To(true) | ||
} | ||
dlipovetsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return bootstrapUser | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.