forked from redhat-developer/kam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.go
90 lines (76 loc) · 2.32 KB
/
utility.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
package utility
import (
"strings"
"github.com/openshift/odo/pkg/log"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
)
// AddGitSuffixIfNecessary will append .git to URL if necessary
func AddGitSuffixIfNecessary(url string) string {
if url == "" || strings.HasSuffix(strings.ToLower(url), ".git") {
return url
}
log.Infof("Adding .git to %s", url)
return url + ".git"
}
// RemoveEmptyStrings returns a slice with all the empty strings removed from the
// source slice.
func RemoveEmptyStrings(s []string) []string {
nonempty := []string{}
for _, v := range s {
if v != "" {
nonempty = append(nonempty, v)
}
}
return nonempty
}
// MaybeCompletePrefix adds a hyphen on the end of the prefix if it doesn't have
// one to make prefix-generated names look a bit nicer.
func MaybeCompletePrefix(s string) string {
if s != "" && !strings.HasSuffix(s, "-") {
return s + "-"
}
return s
}
// Client represents a client for K8s
type Client struct {
KubeClient kubernetes.Interface
}
// NewClient returns a new K8s client
func NewClient(client kubernetes.Interface) *Client {
return &Client{KubeClient: client}
}
// CheckIfSealedSecretsExists checks if sealed secrets is installed
func (c *Client) CheckIfSealedSecretsExists(secret types.NamespacedName) error {
_, err := c.KubeClient.CoreV1().Services(secret.Namespace).Get(secret.Name, v1.GetOptions{})
if err != nil {
return err
}
return nil
}
// CheckIfArgoCDExists checks if ArgoCD operator is installed
func (c *Client) CheckIfArgoCDExists(ns string) error {
_, err := c.KubeClient.AppsV1().Deployments(ns).Get("argocd-operator", v1.GetOptions{})
if err != nil {
return err
}
// check if ArgoCD instance is created
_, err = c.KubeClient.AppsV1().Deployments(ns).Get("argocd-server", v1.GetOptions{})
if err != nil {
return err
}
return err
}
// CheckIfPipelinesExists checks is OpenShift pipelines operator is installed
func (c *Client) CheckIfPipelinesExists(ns string) error {
_, err := c.KubeClient.AppsV1().Deployments(ns).Get("openshift-pipelines-operator", v1.GetOptions{})
if err != nil {
return err
}
return nil
}
// GetFullName generates a command's full name based on its parent's full name and its own name
func GetFullName(parentName, name string) string {
return parentName + " " + name
}