This repository has been archived by the owner on Dec 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
util.go
119 lines (105 loc) · 3.35 KB
/
util.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
package cmak8sutil
import (
"fmt"
cmaVersioned "github.com/samsung-cnct/cma-operator/pkg/generated/cma/client/clientset/versioned"
"github.com/samsung-cnct/cma-operator/pkg/generated/cma/client/clientset/versioned/typed/cma/v1alpha1"
"github.com/spf13/viper"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"os"
"path/filepath"
)
const (
kubeconfigDir = ".kube"
kubeconfigFile = "config"
)
func CreateFromDefaults() (ClientInterface, error) {
output := Client{}
err := output.CreateNewClients()
if err != nil {
return nil, err
}
return &output, nil
}
func (c *Client) CreateNewClients() error {
var err error
if c.config == nil {
err = c.createNewConfig()
if err != nil {
return err
}
}
cmaClient, err := cmaVersioned.NewForConfig(c.config)
if err != nil {
return err
}
c.applicationClient = cmaClient.CmaV1alpha1().SDSApplications(viper.GetString("kubernetes-namespace"))
c.clusterClient = cmaClient.CmaV1alpha1().SDSClusters(viper.GetString("kubernetes-namespace"))
c.packageManagerClient = cmaClient.CmaV1alpha1().SDSPackageManagers(viper.GetString("kubernetes-namespace"))
return err
}
func (c *Client) SetApplicationClient(client v1alpha1.SDSApplicationInterface) {
c.applicationClient = client
}
func (c *Client) SetClusterClient(client v1alpha1.SDSClusterInterface) {
c.clusterClient = client
}
func (c *Client) SetPackageManagerClient(client v1alpha1.SDSPackageManagerInterface) {
c.packageManagerClient = client
}
func (c *Client) createNewConfig() error {
var err error
if c.kubeConfigLocation != "" {
c.config, err = clientcmd.BuildConfigFromFlags("", c.kubeConfigLocation)
} else {
configPath := filepath.Join(homeDir(), kubeconfigDir, kubeconfigFile)
_, err := os.Stat(configPath)
if err == nil {
c.config, err = clientcmd.BuildConfigFromFlags("", configPath)
} else {
c.config, err = rest.InClusterConfig()
}
}
if err != nil {
return err
}
return nil
}
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}
func (c *Client) SetConfig(config *rest.Config) {
c.config = config
}
func (c *Client) getAdjustedName(name string, clusterName string) string {
return name + "-" + clusterName
}
func (c *Client) removeAdjustedName(name string, clusterName string) (string, error) {
inputLength := len(name)
suffixLength := len(clusterName)
if suffixLength >= inputLength {
return "", fmt.Errorf("input string is longer than suffix")
}
if name[(inputLength-suffixLength):] != clusterName {
return "", fmt.Errorf("input string does not have the suffix")
}
return name[0:(inputLength - suffixLength - 1)], nil
}
func (c *Client) getAdjustedApplicationName(name string, packageManagerName string, clusterName string) string {
return name + "-" + packageManagerName + "-" + clusterName
}
func (c *Client) removeAdjustedApplicationName(name string, packageManagerName string, clusterName string) (string, error) {
inputLength := len(name)
suffixLength := len(packageManagerName + "-" + clusterName)
if suffixLength >= inputLength {
return "", fmt.Errorf("input string is longer than suffix")
}
if name[(inputLength-suffixLength):] != packageManagerName+"-"+clusterName {
return "", fmt.Errorf("input string does not have the suffix")
}
return name[0:(inputLength - suffixLength - 1)], nil
}