Skip to content

Commit

Permalink
fixup! clean helm code from bootstrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
elchead committed Aug 1, 2023
1 parent 7660a1a commit 417205d
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 275 deletions.
11 changes: 5 additions & 6 deletions cli/internal/helm/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ go_library(
name = "helm",
srcs = [
"backup.go",
"client.go",
"ciliumhelper.go",
"helm.go",
"helminstaller.go",
"install.go",
"kubernetes_custom.go",
"loader.go",
"release.go",
"serviceversion.go",
"setup.go",
"suite_install.go",
"upgrade.go",
"values.go",
],
embedsrcs = [
Expand Down Expand Up @@ -460,9 +459,9 @@ go_test(
name = "helm_test",
srcs = [
"backup_test.go",
"client_test.go",
"helm_test.go",
"loader_test.go",
"release_test.go",
"upgrade_test.go",
],
data = glob(["testdata/**"]),
embed = [":helm"],
Expand Down
72 changes: 72 additions & 0 deletions cli/internal/helm/ciliumhelper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/

package helm

import (
"context"
"fmt"
"time"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)

type k8sDsClient struct {
clientset *kubernetes.Clientset
}

func newK8sHelmHelper(kubeconfigPath string) (*k8sDsClient, error) {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath)
if err != nil {
return nil, err
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return &k8sDsClient{clientset: clientset}, nil
}

// WaitForDS waits for a DaemonSet to become ready.
func (h *k8sDsClient) WaitForDS(ctx context.Context, namespace, name string, log debugLog) error {
for {
select {
case <-ctx.Done():
return fmt.Errorf("context expired before DaemonSet %q became ready", name)
default:
ds, err := h.clientset.AppsV1().DaemonSets(namespace).Get(ctx, name, v1.GetOptions{})
if err != nil {
return err
}

if ds.Status.NumberReady == ds.Status.DesiredNumberScheduled {
log.Debugf("DaemonSet %s is ready\n", name)
return nil
}

log.Debugf("Waiting for DaemonSet %s to become ready...\n", name)
time.Sleep(10 * time.Second)
}
}
}

// RestartDS restarts all pods of a DaemonSet by updating its template.
func (h *k8sDsClient) RestartDS(namespace, name string) error {
ds, err := h.clientset.AppsV1().DaemonSets(namespace).Get(context.Background(), name, v1.GetOptions{})
if err != nil {
return err
}

ds.Spec.Template.ObjectMeta.Annotations["restartTimestamp"] = fmt.Sprintf("%d", time.Now().Unix())
_, err = h.clientset.AppsV1().DaemonSets(namespace).Update(context.Background(), ds, v1.UpdateOptions{})
if err != nil {
return err
}

return nil
}
22 changes: 22 additions & 0 deletions cli/internal/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,25 @@ It is used by the CLI to:
- create local backups before running service upgrades
*/
package helm

// mergeMaps returns a new map that is the merger of it's inputs.
// Key collisions are resolved by taking the value of the second argument (map b).
// Taken from: https://github.com/helm/helm/blob/dbc6d8e20fe1d58d50e6ed30f09a04a77e4c68db/pkg/cli/values/options.go#L91-L108.
func mergeMaps(a, b map[string]any) map[string]any {
out := make(map[string]any, len(a))
for k, v := range a {
out[k] = v
}
for k, v := range b {
if v, ok := v.(map[string]any); ok {
if bv, ok := out[k]; ok {
if bv, ok := bv.(map[string]any); ok {
out[k] = mergeMaps(bv, v)
continue
}
}
}
out[k] = v
}
return out
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestMergeMaps(t *testing.T) {
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
newVals := MergeMaps(tc.vals, tc.extraVals)
newVals := mergeMaps(tc.vals, tc.extraVals)
assert.Equal(tc.expected, newVals)
})
}
Expand Down
138 changes: 0 additions & 138 deletions cli/internal/helm/helminstaller.go

This file was deleted.

2 changes: 1 addition & 1 deletion cli/internal/helm/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (h *Installer) InstallChart(ctx context.Context, release Release) error {

// InstallChartWithValues is the generic install function for helm charts with custom values.
func (h *Installer) InstallChartWithValues(ctx context.Context, release Release, extraValues map[string]any) error {
mergedVals := MergeMaps(release.Values, extraValues)
mergedVals := mergeMaps(release.Values, extraValues)
h.ReleaseName = release.ReleaseName
if err := h.SetWaitMode(release.WaitMode); err != nil {
return err
Expand Down
104 changes: 0 additions & 104 deletions cli/internal/helm/kubernetes_custom.go

This file was deleted.

Loading

0 comments on commit 417205d

Please sign in to comment.