forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusterdeploy.go
166 lines (135 loc) · 3.94 KB
/
clusterdeploy.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
162
163
164
165
166
package clusterdeploy
import (
"errors"
"fmt"
"reflect"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/clustermanager"
"github.com/rancher/rancher/pkg/kubectl"
"github.com/rancher/rancher/pkg/settings"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/rancher/types/user"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
const (
clusterOwnerRole = "cluster-owner"
)
func Register(management *config.ManagementContext, clusterManager *clustermanager.Manager) {
c := &clusterDeploy{
userManager: management.UserManager,
crtbs: management.Management.ClusterRoleTemplateBindings(""),
clusters: management.Management.Clusters(""),
clusterManager: clusterManager,
}
management.Management.Clusters("").AddHandler("cluster-deploy", c.sync)
}
type clusterDeploy struct {
userManager user.Manager
crtbs v3.ClusterRoleTemplateBindingInterface
clusters v3.ClusterInterface
clusterManager *clustermanager.Manager
}
func (cd *clusterDeploy) sync(key string, cluster *v3.Cluster) error {
var (
err, updateErr error
)
if key == "" {
return nil
}
original := cluster
cluster = original.DeepCopy()
err = cd.doSync(cluster)
if cluster != nil && !reflect.DeepEqual(cluster, original) {
_, updateErr = cd.clusters.Update(cluster)
}
if err != nil {
return err
}
return updateErr
}
func (cd *clusterDeploy) doSync(cluster *v3.Cluster) error {
if !v3.ClusterConditionProvisioned.IsTrue(cluster) {
return nil
}
_, err := v3.ClusterConditionSystemAccountCreated.DoUntilTrue(cluster, func() (runtime.Object, error) {
return cluster, cd.createSystemAccount(cluster)
})
if err != nil {
return err
}
//return cd.deployAgent(cluster)
return nil
}
func (cd *clusterDeploy) createSystemAccount(cluster *v3.Cluster) error {
user, err := cd.getUser(cluster)
if err != nil {
return err
}
bindingName := user.Name + "-admin"
_, err = cd.crtbs.GetNamespaced(cluster.Name, bindingName, v1.GetOptions{})
if err == nil {
return nil
}
_, err = cd.crtbs.Create(&v3.ClusterRoleTemplateBinding{
ObjectMeta: v1.ObjectMeta{
Name: bindingName,
Namespace: cluster.Name,
},
ClusterName: cluster.Name,
UserName: user.Name,
RoleTemplateName: clusterOwnerRole,
})
return err
}
func (cd *clusterDeploy) getUser(cluster *v3.Cluster) (*v3.User, error) {
return cd.userManager.EnsureUser(fmt.Sprintf("system://%s", cluster.Name), "System account for Cluster "+cluster.Spec.DisplayName)
}
func (cd *clusterDeploy) deployAgent(cluster *v3.Cluster) error {
desired := cluster.Spec.DesiredAgentImage
if desired == "" || desired == "fixed" {
desired = settings.AgentImage.Get()
}
if cluster.Status.AgentImage == desired {
return nil
}
kubeConfig, err := cd.getKubeConfig(cluster)
if err != nil {
return err
}
yaml, err := cd.getYAML(cluster, desired)
if err != nil {
return err
}
_, err = v3.ClusterConditionAgentDeployed.Do(cluster, func() (runtime.Object, error) {
output, err := kubectl.Apply(yaml, kubeConfig)
if err != nil {
return cluster, types.NewErrors(err, errors.New(string(output)))
}
v3.ClusterConditionAgentDeployed.Message(cluster, string(output))
return cluster, nil
})
if err == nil {
cluster.Status.AgentImage = desired
if cluster.Spec.DesiredAgentImage == "fixed" {
cluster.Spec.DesiredAgentImage = desired
}
}
return err
}
func (cd *clusterDeploy) getKubeConfig(cluster *v3.Cluster) (*clientcmdapi.Config, error) {
user, err := cd.getUser(cluster)
if err != nil {
return nil, err
}
token, err := cd.userManager.EnsureToken("agent-"+user.Name, "token for agent deployment", user.Name)
if err != nil {
return nil, err
}
return cd.clusterManager.KubeConfig(cluster.Name, token), nil
}
func (cd *clusterDeploy) getYAML(cluster *v3.Cluster, agentImage string) ([]byte, error) {
return nil, nil
}