generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 95
/
meshery_operator.go
110 lines (98 loc) · 2.89 KB
/
meshery_operator.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
package controllers
import (
"context"
"fmt"
"sync"
mesherykube "github.com/layer5io/meshkit/utils/kubernetes"
kubeerror "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kubectl/pkg/polymorphichelpers"
)
type mesheryOperator struct {
name string
status MesheryControllerStatus
client *mesherykube.Client
deploymentConf OperatorDeploymentConfig
mx sync.Mutex
}
type OperatorDeploymentConfig struct {
GetHelmOverrides func(delete bool) map[string]interface{}
HelmChartRepo string
MesheryReleaseVersion string
}
func NewMesheryOperatorHandler(client *mesherykube.Client, deploymentConf OperatorDeploymentConfig) IMesheryController {
return &mesheryOperator{
name: "MesheryOperator",
status: Unknown,
client: client,
deploymentConf: deploymentConf,
}
}
func (mo *mesheryOperator) GetName() string {
return mo.name
}
func (mo *mesheryOperator) GetStatus() MesheryControllerStatus {
if mo.status == Undeployed {
return Undeployed
}
// check if the deployment exists
deployment, err := mo.client.DynamicKubeClient.Resource(schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"}).Namespace("meshery").Get(context.TODO(), "meshery-operator", metav1.GetOptions{})
if err != nil {
if kubeerror.IsNotFound(err) {
mo.setStatus(NotDeployed)
return mo.status
}
return Unknown
}
sv, err := polymorphichelpers.StatusViewerFor(deployment.GroupVersionKind().GroupKind())
if err != nil {
mo.setStatus(Unknown)
return mo.status
}
_, done, err := sv.Status(deployment, 0)
if err != nil {
mo.setStatus(Unknown)
return mo.status
}
if done {
mo.setStatus(Deployed)
} else {
mo.setStatus(Deploying)
}
return mo.status
}
func (mo *mesheryOperator) Deploy(force bool) error {
status := mo.GetStatus()
if status == Undeployed && !force {
return nil
}
if status == Deploying {
return ErrDeployController(fmt.Errorf("Already a Meshery Operator is being deployed."))
}
err := applyOperatorHelmChart(mo.deploymentConf.HelmChartRepo, *mo.client, mo.deploymentConf.MesheryReleaseVersion, false, mo.deploymentConf.GetHelmOverrides(false))
if err != nil {
return ErrDeployController(err)
}
mo.setStatus(Deployed)
return nil
}
func (mo *mesheryOperator) Undeploy() error {
err := applyOperatorHelmChart(mo.deploymentConf.HelmChartRepo, *mo.client, mo.deploymentConf.MesheryReleaseVersion, true, mo.deploymentConf.GetHelmOverrides(false))
if err != nil {
return ErrDeployController(err)
}
mo.setStatus(Undeployed)
return nil
}
func (mo *mesheryOperator) GetPublicEndpoint() (string, error) {
return "", nil
}
func (mo *mesheryOperator) GetVersion() (string, error) {
return "", nil
}
func (mo *mesheryOperator) setStatus(st MesheryControllerStatus) {
mo.mx.Lock()
defer mo.mx.Unlock()
mo.status = st
}