forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusterlocal.go
126 lines (104 loc) · 3.11 KB
/
clusterlocal.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
package clusterlocal
import (
"fmt"
"github.com/rancher/norman/controller"
"github.com/rancher/norman/event"
"github.com/rancher/rancher/pkg/clusterprovisioninglogger"
"github.com/rancher/rancher/pkg/clusteryaml"
"github.com/rancher/rancher/pkg/rkecerts"
"github.com/rancher/rancher/pkg/rkedialerfactory"
"github.com/rancher/rke/cluster"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)
func Register(management *config.ManagementContext) {
cl := &clusterLocal{
clusterController: management.Management.Clusters("").Controller(),
clusters: management.Management.Clusters(""),
eventLogger: management.EventLogger,
nodeLister: management.Management.Nodes("").Controller().Lister(),
rkeDialer: rkedialerfactory.RKEDialerFactory{
Factory: management.Dialer,
},
}
management.Management.Clusters("").AddHandler("cluster-local-provisioner", cl.sync)
management.Management.Nodes("").AddHandler("cluster-local-provisioner", cl.nodeChanged)
}
type clusterLocal struct {
clusterController v3.ClusterController
clusters v3.ClusterInterface
eventLogger event.Logger
nodeLister v3.NodeLister
rkeDialer rkedialerfactory.RKEDialerFactory
}
func (cl *clusterLocal) nodeChanged(key string, node *v3.Node) error {
if node == nil {
return nil
}
cl.clusterController.Enqueue("", node.Namespace)
return nil
}
func (cl *clusterLocal) sync(key string, cluster *v3.Cluster) error {
if cluster == nil {
return nil
}
if cluster.Status.Driver != v3.ClusterDriverLocal {
return nil
}
changed := false
newObj, err := v3.ClusterConditionAddonDeploy.Once(cluster, func() (runtime.Object, error) {
changed = true
if err := cl.deploy(cluster); err != nil {
return nil, err
}
// Need to reload because logger will have changed the cluster object
return cl.clusters.Get(cluster.Name, v1.GetOptions{})
})
if changed && err == nil {
_, err = cl.clusters.Update(newObj.(*v3.Cluster))
}
return err
}
func (cl *clusterLocal) checkNodes(c *v3.Cluster) error {
nodes, err := cl.nodeLister.List(c.Name, labels.Everything())
if err != nil {
return err
}
for _, node := range nodes {
if v3.NodeConditionRegistered.IsTrue(node) {
return nil
}
}
return &controller.ForgetError{
Err: fmt.Errorf("waiting for nodes to join cluster: %s", c.Name),
}
}
func (cl *clusterLocal) deploy(c *v3.Cluster) error {
ctx, logger := clusterprovisioninglogger.NewNonRPCLogger(cl.clusters, cl.eventLogger, c, v3.ClusterConditionAddonDeploy)
defer logger.Close()
if err := cl.checkNodes(c); err != nil {
return err
}
rkeConfig, err := clusteryaml.LocalConfig()
if err != nil {
return err
}
wt := cl.rkeDialer.WrapTransport(rkeConfig)
if err := cluster.ApplyAuthzResources(ctx, *rkeConfig, "", "", wt); err != nil {
return err
}
bundle, err := rkecerts.Load()
if err != nil {
return err
}
return cluster.ConfigureCluster(ctx,
*rkeConfig,
bundle.Certs(),
"",
"",
cl.rkeDialer.WrapTransport(rkeConfig),
true)
}