Skip to content

Commit 29593da

Browse files
committed
chore: some cleanups
1 parent f6c484b commit 29593da

File tree

6 files changed

+115
-145
lines changed

6 files changed

+115
-145
lines changed

kubernetes-services/templates/argo-rollouts.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ spec:
2323
prune: true
2424
selfHeal: true
2525
syncOptions:
26-
- CreateNamespace=true
26+
- CreateNamespace=true

pkg/helm.go

Lines changed: 0 additions & 114 deletions
This file was deleted.

pkg/helm/helm.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package helm
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
9+
"e2eutils/pkg/argo"
10+
11+
"sigs.k8s.io/e2e-framework/third_party/helm"
12+
)
13+
14+
type HelmArguments struct {
15+
Name string
16+
Namespace string
17+
Chart string
18+
Version string
19+
ValuesFilePath string
20+
OciRepository string
21+
}
22+
23+
func NewHelmManager(kubeConfigFile string) *helm.Manager {
24+
return helm.New(kubeConfigFile)
25+
}
26+
27+
func AddHelmRepository(helmMgr *helm.Manager, repoURL, chartName string) error {
28+
return helmMgr.RunRepo(helm.WithArgs("add", "--force-update", chartName, repoURL))
29+
}
30+
31+
func DeployHelmChart(helmMgr *helm.Manager, src argo.ApplicationSource, namespace string) error {
32+
opts, err := buildHelmArguments(src, namespace)
33+
if err != nil {
34+
return fmt.Errorf("building helm options: %w", err)
35+
}
36+
37+
options := []helm.Option{
38+
helm.WithName(opts.Name),
39+
helm.WithNamespace(opts.Namespace),
40+
helm.WithChart(opts.Chart),
41+
helm.WithVersion(opts.Version),
42+
helm.WithArgs("--install", "--create-namespace"),
43+
}
44+
45+
if opts.ValuesFilePath != "" {
46+
options = append(options, helm.WithArgs("-f", opts.ValuesFilePath))
47+
}
48+
49+
if err := helmMgr.RunUpgrade(options...); err != nil {
50+
return fmt.Errorf("helm upgrade/install failed: %w", err)
51+
}
52+
53+
return nil
54+
}
55+
56+
func buildHelmArguments(src argo.ApplicationSource, namespace string) (*HelmArguments, error) {
57+
opts := &HelmArguments{
58+
Name: src.Chart,
59+
Namespace: namespace,
60+
Version: src.TargetRevision,
61+
}
62+
63+
if strings.HasPrefix(src.RepoURL, "oci://") {
64+
base := strings.TrimSuffix(src.RepoURL, "/")
65+
opts.Chart = fmt.Sprintf("%s/%s", base, src.Chart)
66+
opts.OciRepository = ""
67+
} else {
68+
opts.Chart = fmt.Sprintf("%s/%s", src.Chart, src.Chart)
69+
}
70+
71+
if src.Helm != nil && src.Helm.Values != "" {
72+
valuesFilePath, err := writeValuesFile(src.Chart, src.TargetRevision, src.Helm.Values)
73+
if err != nil {
74+
return nil, fmt.Errorf("writing values file: %w", err)
75+
}
76+
opts.ValuesFilePath = valuesFilePath
77+
}
78+
79+
return opts, nil
80+
}
81+
82+
func writeValuesFile(chart, revision, valuesContent string) (string, error) {
83+
valuesFilePath := fmt.Sprintf("values-%s-%s.yaml", chart, revision)
84+
filePath := filepath.Join(os.TempDir(), valuesFilePath)
85+
if err := os.WriteFile(filePath, []byte(valuesContent), 0o600); err != nil {
86+
return "", err
87+
}
88+
89+
return filePath, nil
90+
}

pkg/kustomize.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@ func GetKubernetesManifests(argoApplication argo.Application) ([]k8s.Object, err
1616
var objects []k8s.Object
1717
var err error
1818

19-
if argoApplication.Spec.Source != nil {
20-
if argoApplication.Spec.Source.Path == "" {
21-
return nil, nil
22-
}
23-
19+
if argoApplication.Spec.Source != nil && argoApplication.Spec.Source.Path != "" {
2420
objects, err = prepareKubernetesManifests(*argoApplication.Spec.Source)
2521
if err != nil {
2622
return nil, err
2723
}
24+
25+
return objects, nil
2826
}
2927

3028
var source argo.ApplicationSource

pkg/test.go

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package e2eutils
33
import (
44
"context"
55
"e2eutils/pkg/argo"
6+
"e2eutils/pkg/helm"
67
"errors"
78
"log/slog"
89
"reflect"
@@ -72,30 +73,8 @@ func PrepareArgoApp(gitRepository string, applicationYaml string) (argo.Applicat
7273
return current, update, objects, nil
7374
}
7475

75-
func deployHelmChart(applicationSource argo.ApplicationSource, namespace string, kubeConfigFile string) error {
76-
helmMgr := GetHelmManager(kubeConfigFile)
77-
78-
if !strings.Contains(applicationSource.RepoURL, "oci://") {
79-
err := AddHelmRepository(helmMgr, applicationSource.RepoURL, applicationSource.Chart)
80-
if err != nil {
81-
return err
82-
}
83-
}
84-
85-
err := DeployHelmChart(helmMgr, applicationSource, namespace)
86-
if err != nil {
87-
return err
88-
}
89-
90-
return nil
91-
}
92-
9376
func DeployHelmCharts(kubeConfigFile string, argoApplication argo.Application) error {
94-
if argoApplication.Spec.Source != nil {
95-
if argoApplication.Spec.Source.Chart == "" {
96-
return nil
97-
}
98-
77+
if argoApplication.Spec.Source != nil && argoApplication.Spec.Source.Chart != "" {
9978
err := deployHelmChart(*argoApplication.Spec.Source, argoApplication.Spec.Destination.Namespace, kubeConfigFile)
10079
if err != nil {
10180
return errors.New(err.Error())
@@ -119,6 +98,24 @@ func DeployHelmCharts(kubeConfigFile string, argoApplication argo.Application) e
11998
return nil
12099
}
121100

101+
func deployHelmChart(applicationSource argo.ApplicationSource, namespace string, kubeConfigFile string) error {
102+
helmMgr := helm.NewHelmManager(kubeConfigFile)
103+
104+
if !strings.HasPrefix(applicationSource.RepoURL, "oci://") {
105+
err := helm.AddHelmRepository(helmMgr, applicationSource.RepoURL, applicationSource.Chart)
106+
if err != nil {
107+
return err
108+
}
109+
}
110+
111+
err := helm.DeployHelmChart(helmMgr, applicationSource, namespace)
112+
if err != nil {
113+
return err
114+
}
115+
116+
return nil
117+
}
118+
122119
func CheckJobsCompleted(ctx context.Context, client klient.Client, namespace string) error {
123120
kubeConfig := client.RESTConfig()
124121
clientSet, err := kubernetes.NewForConfig(kubeConfig)
@@ -183,7 +180,7 @@ func DaemonSetBecameReady(ctx context.Context, client klient.Client, namespace s
183180
return err
184181
}
185182

186-
daemonSetList, err := clientSet.AppsV1().DaemonSets(namespace).List(context.TODO(), metav1.ListOptions{})
183+
daemonSetList, err := clientSet.AppsV1().DaemonSets(namespace).List(ctx, metav1.ListOptions{})
187184
if err != nil {
188185
return err
189186
}

test/k3s_system_upgrade_controller_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ func TestK3sSystemUpgradeController(t *testing.T) {
4848
continue
4949
}
5050

51-
namespace = object.GetName()
5251
err = e2eutils.Apply(*clientSet, &object)
5352
require.NoError(t, err)
5453

0 commit comments

Comments
 (0)