This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
103 lines (89 loc) · 3.38 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"context"
"encoding/base64"
"flag"
"fmt"
"github.com/Azure/helmrelease-workflow-executor/pkg/actions"
"time"
fluxhelmv2beta1 "github.com/fluxcd/helm-controller/api/v2beta1"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/kubectl/pkg/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"
)
const (
// The set of executor actions which can be performed on a helmrelease object
Install ExecutorAction = "install"
Delete ExecutorAction = "delete"
)
// ExecutorAction defines the set of executor actions which can be performed on a helmrelease object
type ExecutorAction string
func ParseExecutorAction(s string) (ExecutorAction, error) {
a := ExecutorAction(s)
switch a {
case Install, Delete:
return a, nil
}
return "", fmt.Errorf("invalid executor action: %v", s)
}
func main() {
var spec string
var actionStr string
var timeoutStr string
var intervalStr string
flag.StringVar(&spec, "spec", "", "Spec of the helmrelease object to apply")
flag.StringVar(&actionStr, "action", "", "Action to perform on the helmrelease object. Must be either install or delete")
flag.StringVar(&timeoutStr, "timeout", "5m", "Timeout for the execution of the argo workflow task")
flag.StringVar(&intervalStr, "interval", "10s", "Retry interval for the all actions by the executor")
flag.Parse()
action, err := ParseExecutorAction(actionStr)
if err != nil {
log.Fatalf("Failed to parse action as an executor action with %v", err)
}
timeout, err := time.ParseDuration(timeoutStr)
if err != nil {
log.Fatalf("Failed to parse timeout as a duration with %v", err)
}
interval, err := time.ParseDuration(intervalStr)
if err != nil {
log.Fatalf("Failed to parse interval as a duration with %v", err)
}
log.Infof("Parsed the action: %v, the timeout: %v and the interval: %v", string(action), timeout.String(), interval.String())
ctx, cancel := context.WithTimeout(context.Background(), timeout)
if spec == "" {
log.Fatal("Spec is empty, unable to apply an empty spec on the cluster")
}
decodedSpec, err := base64.StdEncoding.DecodeString(spec)
if err != nil {
log.Fatalf("Failed to decode the string as a base64 string; got the string %v", spec)
}
log.Info("Successfully base64 decoded the spec")
hr := &fluxhelmv2beta1.HelmRelease{}
if err := yaml.Unmarshal(decodedSpec, hr); err != nil {
log.Fatalf("Failed to decode the spec into yaml with the err %v", err)
}
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{})
config, err := kubeConfig.ClientConfig()
if err != nil {
log.Fatalf("Failed to initialize the client config with %v", err)
}
k8sScheme := scheme.Scheme
if err := fluxhelmv2beta1.AddToScheme(k8sScheme); err != nil {
log.Fatalf("Failed to add the flux helm scheme to the configuration scheme with %v", err)
}
clientSet, err := client.New(config, client.Options{Scheme: k8sScheme})
if err != nil {
log.Fatalf("Failed to create the clientset with the given config with %v", err)
}
if action == Install {
if err := actions.Install(ctx, cancel, clientSet, hr, interval); err != nil {
log.Fatalf("failed to install the helm release: %v", err)
}
} else if action == Delete {
if err := actions.Delete(ctx, cancel, clientSet, hr, interval); err != nil {
log.Fatalf("failed to delete the helm release: %v", err)
}
}
}