-
Notifications
You must be signed in to change notification settings - Fork 90
/
deploy.go
161 lines (136 loc) · 4.45 KB
/
deploy.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
/*
Copyright 2021 The Cockroach Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package actor
import (
"context"
api "github.com/cockroachdb/cockroach-operator/apis/v1alpha1"
"github.com/cockroachdb/cockroach-operator/pkg/condition"
"github.com/cockroachdb/cockroach-operator/pkg/features"
"github.com/cockroachdb/cockroach-operator/pkg/kube"
"github.com/cockroachdb/cockroach-operator/pkg/resource"
"github.com/cockroachdb/cockroach-operator/pkg/utilfeature"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func newDeploy(scheme *runtime.Scheme, cl client.Client, config *rest.Config, kd kube.KubernetesDistribution) Actor {
return &deploy{
action: newAction("deploy", scheme, cl),
config: config,
kd: kd,
}
}
// deploy initializes and reconciles the Kubernetes resources needed by the CockroachDB cluster:
// services, a statefulset and a pod disruption budget
type deploy struct {
action
config *rest.Config
kd kube.KubernetesDistribution
}
// GetActionType returns the api.DeployAction value used to set the cluster status errors
func (d deploy) GetActionType() api.ActionType {
return api.DeployAction
}
func (d deploy) Handles(conds []api.ClusterCondition) bool {
if utilfeature.DefaultMutableFeatureGate.Enabled(features.CrdbVersionValidator) {
return (condition.True(api.InitializedCondition, conds) || condition.False(api.InitializedCondition, conds)) && (condition.True(api.CrdbVersionChecked, conds))
}
return (condition.True(api.InitializedCondition, conds) || condition.False(api.InitializedCondition, conds))
}
func (d deploy) Act(ctx context.Context, cluster *resource.Cluster) error {
log := d.log.WithValues("CrdbCluster", cluster.ObjectKey())
log.V(DEBUGLEVEL).Info("reconciling resources on deploy action")
r := resource.NewManagedKubeResource(ctx, d.client, cluster, kube.AnnotatingPersister)
owner := cluster.Unwrap()
kubernetesDistro, err := d.kd.Get(ctx, d.config, log)
if err != nil {
return errors.Wrap(err, "failed to get Kubernetes distribution")
}
kubernetesDistro = "kubernetes-operator-" + kubernetesDistro
changed, err := (resource.Reconciler{
ManagedResource: r,
Builder: resource.DiscoveryServiceBuilder{
Cluster: cluster,
Selector: r.Labels.Selector(),
},
Owner: owner,
Scheme: d.scheme,
}).Reconcile()
if err != nil {
return errors.Wrap(err, "failed to reconcile discovery service")
}
if changed {
log.Info("created/updated discovery service, stopping request processing")
CancelLoop(ctx)
return nil
}
changed, err = (resource.Reconciler{
ManagedResource: r,
Builder: resource.PublicServiceBuilder{
Cluster: cluster,
Selector: r.Labels.Selector(),
},
Owner: owner,
Scheme: d.scheme,
}).Reconcile()
if err != nil {
return errors.Wrap(err, "failed to reconcile public service")
}
if changed {
log.Info("created/updated public service, stopping request processing")
CancelLoop(ctx)
return nil
}
changed, err = (resource.Reconciler{
ManagedResource: r,
Builder: resource.StatefulSetBuilder{
Cluster: cluster,
Selector: r.Labels.Selector(),
Telemetry: kubernetesDistro,
},
Owner: owner,
Scheme: d.scheme,
}).Reconcile()
if err != nil {
return errors.Wrap(err, "failed to reconcile statefulset")
}
if changed {
log.Info("created/updated statefulset, stopping request processing")
CancelLoop(ctx)
return nil
}
// if we only have one Node we cannot have a PDB
// TODO we need to validate this in the CRD API
if cluster.Spec().Nodes > 1 {
changed, err = (resource.Reconciler{
ManagedResource: r,
Builder: resource.PdbBuilder{
Cluster: cluster,
Selector: r.Labels.Selector(),
},
Owner: owner,
Scheme: d.scheme,
}).Reconcile()
if err != nil {
return errors.Wrap(err, "failed to reconcile pdb")
}
if changed {
log.Info("created/updated pdb, stopping request processing")
CancelLoop(ctx)
return nil
}
}
log.Info("deployed database")
return nil
}