-
Notifications
You must be signed in to change notification settings - Fork 25
/
helpers.go
161 lines (143 loc) · 4.48 KB
/
helpers.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package common
import (
"encoding/json"
"fmt"
"strings"
"time"
wfv1versioned "github.com/argoproj/argo-workflows/v3/pkg/client/clientset/versioned"
addonv1versioned "github.com/keikoproj/addon-manager/pkg/client/clientset/versioned"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
addonv1 "github.com/keikoproj/addon-manager/api/addon/v1alpha1"
)
// ContainsString helper function to check string in a slice of strings.
func ContainsString(slice []string, s string) bool {
for _, item := range slice {
if item == s {
return true
}
}
return false
}
// RemoveString helper function to remove a string in a slice of strings.
func RemoveString(slice []string, s string) (result []string) {
for _, item := range slice {
if item == s {
continue
}
result = append(result, item)
}
return
}
// GetCurrentTimestamp -- get current timestamp in millisecond
func GetCurrentTimestamp() int64 {
return time.Now().UnixMilli()
}
// IsExpired --- check if reached ttl time
func IsExpired(startTime int64, ttlTime int64) bool {
if GetCurrentTimestamp()-startTime >= ttlTime {
return true
}
return false
}
// NewWFClient -- declare new workflow client
func NewWFClient(cfg *rest.Config) wfv1versioned.Interface {
cli, err := wfv1versioned.NewForConfig(cfg)
if err != nil {
return nil
}
return cli
}
// NewAddonClient - declare new addon client
func NewAddonClient(cfg *rest.Config) addonv1versioned.Interface {
cli, err := addonv1versioned.NewForConfig(cfg)
if err != nil {
return nil
}
return cli
}
func WorkFlowFromUnstructured(un *unstructured.Unstructured) (*wfv1.Workflow, error) {
var wf wfv1.Workflow
err := FromUnstructuredObj(un, &wf)
return &wf, err
}
func FromUnstructured(un *unstructured.Unstructured) (*addonv1.Addon, error) {
var addon addonv1.Addon
err := FromUnstructuredObj(un, &addon)
return &addon, err
}
// FromUnstructuredObj convert unstructured to objects
func FromUnstructuredObj(un *unstructured.Unstructured, v interface{}) error {
err := runtime.DefaultUnstructuredConverter.FromUnstructured(un.Object, v)
if err != nil {
if err.Error() == "cannot convert int64 to v1alpha1.AnyString" {
data, err := json.Marshal(un)
if err != nil {
return err
}
return json.Unmarshal(data, v)
}
return err
}
return nil
}
func ConvertWorkflowPhaseToAddonPhase(lifecycle addonv1.LifecycleStep, phase wfv1.WorkflowPhase) addonv1.ApplicationAssemblyPhase {
switch phase {
case wfv1.WorkflowPending, wfv1.WorkflowRunning:
if lifecycle == addonv1.Delete {
return addonv1.Deleting
}
return addonv1.Pending
case wfv1.WorkflowSucceeded:
if lifecycle == addonv1.Delete {
return addonv1.DeleteSucceeded
}
return addonv1.Succeeded
case wfv1.WorkflowFailed, wfv1.WorkflowError:
if lifecycle == addonv1.Delete {
return addonv1.DeleteFailed
}
return addonv1.Failed
default:
return ""
}
}
// ExtractChecksumAndLifecycleStep extracts the checksum and lifecycle step from the workflow name
func ExtractChecksumAndLifecycleStep(addonWorkflowName string) (string, addonv1.LifecycleStep, error) {
// addonWorkflowName is of the form <addon-name>-<lifecycle>-<checksum>-wf
// e.g. my-addon-prereqs-12345678-wf
wfParts := strings.Split(addonWorkflowName, "-")
if len(wfParts) < 4 || strings.TrimSpace(wfParts[len(wfParts)-1]) != "wf" {
return "", "", fmt.Errorf("invalid workflow name %s", addonWorkflowName)
}
var checksum = strings.TrimSpace(wfParts[len(wfParts)-2])
var lifecycle addonv1.LifecycleStep
switch strings.TrimSpace(wfParts[len(wfParts)-3]) {
case "prereqs":
lifecycle = addonv1.Prereqs
case "install":
lifecycle = addonv1.Install
case "validate":
lifecycle = addonv1.Validate
case "delete":
lifecycle = addonv1.Delete
default:
return "", "", fmt.Errorf("invalid lifecycle in workflow name %s", addonWorkflowName)
}
return checksum, lifecycle, nil
}