Skip to content
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

Set SnapInstallData from CK8sConfigSpec #62

Merged
merged 21 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions bootstrap/api/v1beta2/ck8sconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ type CK8sConfigSpec struct {
// +optional
SnapstoreProxyID string `json:"snapstoreProxyId,omitempty"`

// Channel is the channel to use for the snap install.
// +optional
Channel string `json:"channel,omitempty"`

// Revision is the revision to use for the snap install.
// If Channel is set, this will be ignored.
eaudetcobello marked this conversation as resolved.
Show resolved Hide resolved
// +optional
Revision string `json:"revision,omitempty"`

// LocalPath is the path of a local snap file in the workload cluster to use for the snap install.
// If Channel or Revision are set, this will be ignored.
// +optional
LocalPath string `json:"localPath,omitempty"`
berkayoz marked this conversation as resolved.
Show resolved Hide resolved

// CK8sControlPlaneConfig is configuration for the control plane node.
// +optional
ControlPlaneConfig CK8sControlPlaneConfig `json:"controlPlane,omitempty"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ spec:
items:
type: string
type: array
channel:
description: Channel is the channel to use for the snap install.
type: string
controlPlane:
description: CK8sControlPlaneConfig is configuration for the control
plane node.
Expand Down Expand Up @@ -192,6 +195,11 @@ spec:
the default CNI.
type: boolean
type: object
localPath:
description: |-
LocalPath is the path of a local snap file in the workload cluster to use for the snap install.
If Channel or Revision are set, this will be ignored.
type: string
nodeName:
description: |-
NodeName is the name to use for the kubelet of this node. It is needed for clouds
Expand All @@ -210,6 +218,11 @@ spec:
items:
type: string
type: array
revision:
description: |-
Revision is the revision to use for the snap install.
If Channel is set, this will be ignored.
type: string
snapstoreProxyDomain:
description: The snap store proxy domain
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ spec:
items:
type: string
type: array
channel:
description: Channel is the channel to use for the snap install.
type: string
controlPlane:
description: CK8sControlPlaneConfig is configuration for the
control plane node.
Expand Down Expand Up @@ -201,6 +204,11 @@ spec:
enable the default CNI.
type: boolean
type: object
localPath:
description: |-
LocalPath is the path of a local snap file in the workload cluster to use for the snap install.
If Channel or Revision are set, this will be ignored.
type: string
nodeName:
description: |-
NodeName is the name to use for the kubelet of this node. It is needed for clouds
Expand All @@ -219,6 +227,11 @@ spec:
items:
type: string
type: array
revision:
description: |-
Revision is the revision to use for the snap install.
If Channel is set, this will be ignored.
type: string
snapstoreProxyDomain:
description: The snap store proxy domain
type: string
Expand Down
91 changes: 66 additions & 25 deletions bootstrap/controllers/ck8sconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,13 @@ func (r *CK8sConfigReconciler) joinControlplane(ctx context.Context, scope *Scop
return err
}

snapInstallData := r.resolveInPlaceUpgradeRelease(machine)
snapInstallData := r.getSnapInstallDataFromSpec(scope.Config.Spec)
eaudetcobello marked this conversation as resolved.
Show resolved Hide resolved

// If the machine has an in-place upgrade annotation, use it to set the snap install data
inPlaceInstallData := r.resolveInPlaceUpgradeRelease(machine)
if inPlaceInstallData.Option != "" && inPlaceInstallData.Value != "" {
snapInstallData = inPlaceInstallData
}

input := cloudinit.JoinControlPlaneInput{
BaseUserData: cloudinit.BaseUserData{
Expand Down Expand Up @@ -343,7 +349,13 @@ func (r *CK8sConfigReconciler) joinWorker(ctx context.Context, scope *Scope) err
return err
}

snapInstallData := r.resolveInPlaceUpgradeRelease(machine)
snapInstallData := r.getSnapInstallDataFromSpec(scope.Config.Spec)

// If the machine has an in-place upgrade annotation, use it to set the snap install data
inPlaceInstallData := r.resolveInPlaceUpgradeRelease(machine)
if inPlaceInstallData.Option != "" && inPlaceInstallData.Value != "" {
snapInstallData = inPlaceInstallData
}

input := cloudinit.JoinWorkerInput{
BaseUserData: cloudinit.BaseUserData{
Expand Down Expand Up @@ -406,38 +418,67 @@ func (r *CK8sConfigReconciler) resolveFiles(ctx context.Context, cfg *bootstrapv
func (r *CK8sConfigReconciler) resolveInPlaceUpgradeRelease(machine *clusterv1.Machine) cloudinit.SnapInstallData {
mAnnotations := machine.GetAnnotations()

if mAnnotations != nil {
if mAnnotations == nil {
return cloudinit.SnapInstallData{}
berkayoz marked this conversation as resolved.
Show resolved Hide resolved
}

val, ok := mAnnotations[bootstrapv1.InPlaceUpgradeReleaseAnnotation]
if ok {
optionKv := strings.Split(val, "=")

switch optionKv[0] {
case "channel":
return cloudinit.SnapInstallData{
Option: cloudinit.InstallOptionChannel,
Value: optionKv[1],
}
case "revision":
return cloudinit.SnapInstallData{
Option: cloudinit.InstallOptionRevision,
Value: optionKv[1],
}
case "localPath":
return cloudinit.SnapInstallData{
Option: cloudinit.InstallOptionLocalPath,
Value: optionKv[1],
}
default:
r.Log.Info("Unknown in-place upgrade release option, ignoring", "option", optionKv[0])
if !ok {
return cloudinit.SnapInstallData{}
}

optionKv := strings.Split(val, "=")

if len(optionKv) != 2 {
r.Log.Info("Invalid in-place upgrade release annotation, ignoring", "annotation", val)
return cloudinit.SnapInstallData{}
}

switch optionKv[0] {
case "channel":
return cloudinit.SnapInstallData{
Option: cloudinit.InstallOptionChannel,
Value: optionKv[1],
}
case "revision":
return cloudinit.SnapInstallData{
Option: cloudinit.InstallOptionRevision,
Value: optionKv[1],
}
case "localPath":
return cloudinit.SnapInstallData{
Option: cloudinit.InstallOptionLocalPath,
Value: optionKv[1],
}
default:
r.Log.Info("Unknown in-place upgrade release option, ignoring", "option", optionKv[0])
}

return cloudinit.SnapInstallData{}
}

func (r *CK8sConfigReconciler) getSnapInstallDataFromSpec(spec bootstrapv1.CK8sConfigSpec) cloudinit.SnapInstallData {
switch {
case spec.Channel != "":
return cloudinit.SnapInstallData{
Option: cloudinit.InstallOptionChannel,
Value: spec.Channel,
}
case spec.Revision != "":
return cloudinit.SnapInstallData{
Option: cloudinit.InstallOptionRevision,
Value: spec.Revision,
}
case spec.LocalPath != "":
return cloudinit.SnapInstallData{
Option: cloudinit.InstallOptionLocalPath,
Value: spec.LocalPath,
}
default:
return cloudinit.SnapInstallData{}
}
}

// resolveSecretFileContent returns file content fetched from a referenced secret object.
func (r *CK8sConfigReconciler) resolveSecretFileContent(ctx context.Context, ns string, source bootstrapv1.File) ([]byte, error) {
secret := &corev1.Secret{}
Expand Down Expand Up @@ -576,7 +617,7 @@ func (r *CK8sConfigReconciler) handleClusterNotInitialized(ctx context.Context,
return ctrl.Result{}, fmt.Errorf("failed to render k8sd-proxy daemonset: %w", err)
}

snapInstallData := r.resolveInPlaceUpgradeRelease(machine)
snapInstallData := r.getSnapInstallDataFromSpec(scope.Config.Spec)

cpinput := cloudinit.InitControlPlaneInput{
BaseUserData: cloudinit.BaseUserData{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ spec:
items:
type: string
type: array
channel:
description: Channel is the channel to use for the snap install.
type: string
controlPlane:
description: CK8sControlPlaneConfig is configuration for the control
plane node.
Expand Down Expand Up @@ -389,6 +392,11 @@ spec:
the default CNI.
type: boolean
type: object
localPath:
description: |-
LocalPath is the path of a local snap file in the workload cluster to use for the snap install.
If Channel or Revision are set, this will be ignored.
type: string
nodeName:
description: |-
NodeName is the name to use for the kubelet of this node. It is needed for clouds
Expand All @@ -407,6 +415,11 @@ spec:
items:
type: string
type: array
revision:
description: |-
Revision is the revision to use for the snap install.
If Channel is set, this will be ignored.
type: string
snapstoreProxyDomain:
description: The snap store proxy domain
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ spec:
items:
type: string
type: array
channel:
description: Channel is the channel to use for the snap
install.
type: string
controlPlane:
description: CK8sControlPlaneConfig is configuration for
the control plane node.
Expand Down Expand Up @@ -366,6 +370,11 @@ spec:
to enable the default CNI.
type: boolean
type: object
localPath:
description: |-
LocalPath is the path of a local snap file in the workload cluster to use for the snap install.
If Channel or Revision are set, this will be ignored.
type: string
nodeName:
description: |-
NodeName is the name to use for the kubelet of this node. It is needed for clouds
Expand All @@ -384,6 +393,11 @@ spec:
items:
type: string
type: array
revision:
description: |-
Revision is the revision to use for the snap install.
If Channel is set, this will be ignored.
type: string
snapstoreProxyDomain:
description: The snap store proxy domain
type: string
Expand Down
5 changes: 5 additions & 0 deletions pkg/ck8s/workload_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ func (w *Workload) RefreshMachine(ctx context.Context, machine *clusterv1.Machin
request := apiv1.SnapRefreshRequest{}
response := &apiv1.SnapRefreshResponse{}
optionKv := strings.Split(upgradeOption, "=")

if len(optionKv) != 2 {
return "", fmt.Errorf("invalid in-place upgrade release annotation: %s", upgradeOption)
}

switch optionKv[0] {
case "channel":
request.Channel = optionKv[1]
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudinit/controlplane_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestNewInitControlPlaneSnapInstall(t *testing.T) {
name: "ChannelOverride",
snapInstall: cloudinit.SnapInstallData{
Option: cloudinit.InstallOptionChannel,
Value: "v1.30/stable",
Value: "v1.30/edge",
},
},
{
Expand Down