Skip to content

Commit 92198ec

Browse files
authored
[Bugfix] Use persistent name and namespace (#744)
1 parent b29207e commit 92198ec

File tree

9 files changed

+86
-19
lines changed

9 files changed

+86
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Change Log
22

33
## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A)
4+
- Use persistent name and namespace in ArangoDeployment reconcilation loop
45

56
## [1.1.9](https://github.com/arangodb/kube-arangodb/tree/1.1.9) (2021-05-28)
67
- Add IP, DNS, ShortDNS, HeadlessService (Default) communication methods

pkg/apis/deployment/v2alpha1/deployment_spec.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,46 @@ func validatePullPolicy(v core.PullPolicy) error {
5050
}
5151
}
5252

53+
// DeploymentCommunicationMethod define communication method used for inter-cluster communication
54+
type DeploymentCommunicationMethod string
55+
56+
// Get returns communication method from pointer. If pointer is nil default is returned.
57+
func (d *DeploymentCommunicationMethod) Get() DeploymentCommunicationMethod {
58+
if d == nil {
59+
return DefaultDeploymentCommunicationMethod
60+
}
61+
62+
switch v := *d; v {
63+
case DeploymentCommunicationMethodHeadlessService, DeploymentCommunicationMethodDNS, DeploymentCommunicationMethodIP, DeploymentCommunicationMethodShortDNS:
64+
return v
65+
default:
66+
return DefaultDeploymentCommunicationMethod
67+
}
68+
}
69+
70+
// String returns string representation of method.
71+
func (d DeploymentCommunicationMethod) String() string {
72+
return string(d)
73+
}
74+
75+
// New returns pointer.
76+
func (d DeploymentCommunicationMethod) New() *DeploymentCommunicationMethod {
77+
return &d
78+
}
79+
80+
const (
81+
// DefaultDeploymentCommunicationMethod define default communication method.
82+
DefaultDeploymentCommunicationMethod = DeploymentCommunicationMethodHeadlessService
83+
// DeploymentCommunicationMethodHeadlessService define old communication mechanism, based on headless service.
84+
DeploymentCommunicationMethodHeadlessService DeploymentCommunicationMethod = "headless"
85+
// DeploymentCommunicationMethodDNS define ClusterIP Service DNS based communication.
86+
DeploymentCommunicationMethodDNS DeploymentCommunicationMethod = "dns"
87+
// DeploymentCommunicationMethodDNS define ClusterIP Service DNS based communication. Use namespaced short DNS (used in migration)
88+
DeploymentCommunicationMethodShortDNS DeploymentCommunicationMethod = "short-dns"
89+
// DeploymentCommunicationMethodIP define ClusterIP Servce IP based communication.
90+
DeploymentCommunicationMethodIP DeploymentCommunicationMethod = "ip"
91+
)
92+
5393
// DeploymentSpec contains the spec part of a ArangoDeployment resource.
5494
type DeploymentSpec struct {
5595
Mode *DeploymentMode `json:"mode,omitempty"`
@@ -118,6 +158,9 @@ type DeploymentSpec struct {
118158
Timeouts *Timeouts `json:"timeouts,omitempty"`
119159

120160
ClusterDomain *string `json:"ClusterDomain,omitempty"`
161+
162+
// CommunicationMethod define communication method used in deployment
163+
CommunicationMethod *DeploymentCommunicationMethod `json:"communicationMethod,omitempty"`
121164
}
122165

123166
// GetRestoreFrom returns the restore from string or empty string if not set

pkg/apis/deployment/v2alpha1/server_group.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ func (g *ServerGroup) UnmarshalJSON(bytes []byte) error {
3535
return nil
3636
}
3737

38+
{
39+
// Try with int
40+
var s int
41+
42+
if err := json.Unmarshal(bytes, &s); err == nil {
43+
*g = ServerGroupFromRole(ServerGroup(s).AsRole())
44+
return nil
45+
}
46+
}
47+
3848
var s string
3949

4050
if err := json.Unmarshal(bytes, &s); err != nil {

pkg/apis/deployment/v2alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/deployment/context_impl.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ func (d *Deployment) GetOperatorUUIDImage() string {
114114
return d.config.OperatorUUIDInitImage
115115
}
116116

117-
// GetNamespSecretsInterfaceace returns the kubernetes namespace that contains
117+
// GetNamespace returns the kubernetes namespace that contains
118118
// this deployment.
119119
func (d *Deployment) GetNamespace() string {
120-
return d.apiObject.GetNamespace()
120+
return d.namespace
121121
}
122122

123123
// GetPhase returns the current phase of the deployment
@@ -275,7 +275,7 @@ func (d *Deployment) getAuth() (driver.Authentication, error) {
275275
return nil, nil
276276
}
277277

278-
var secrets secret.ReadInterface = d.GetKubeCli().CoreV1().Secrets(d.apiObject.GetNamespace())
278+
var secrets secret.ReadInterface = d.GetKubeCli().CoreV1().Secrets(d.GetNamespace())
279279
if currentState := d.currentState; currentState != nil {
280280
secrets = currentState.SecretReadInterface()
281281
}
@@ -294,7 +294,7 @@ func (d *Deployment) getAuth() (driver.Authentication, error) {
294294

295295
secret = string(jwt)
296296
} else {
297-
s, err := secrets.Get(context.Background(), pod.JWTSecretFolder(d.apiObject.GetName()), meta.GetOptions{})
297+
s, err := secrets.Get(context.Background(), pod.JWTSecretFolder(d.GetName()), meta.GetOptions{})
298298
if err != nil {
299299
d.deps.Log.Error().Err(err).Msgf("Unable to get secret")
300300
return nil, errors.Newf("JWT Folder Secret is missing")
@@ -327,7 +327,7 @@ func (d *Deployment) GetSyncServerClient(ctx context.Context, group api.ServerGr
327327
// Fetch monitoring token
328328
log := d.deps.Log
329329
kubecli := d.deps.KubeCli
330-
ns := d.apiObject.GetNamespace()
330+
ns := d.GetNamespace()
331331
secrets := kubecli.CoreV1().Secrets(ns)
332332
secretName := d.apiObject.Spec.Sync.Monitoring.GetTokenSecretName()
333333
monitoringToken, err := k8sutil.GetTokenSecret(ctx, secrets, secretName)
@@ -392,7 +392,7 @@ func (d *Deployment) GetPod(ctx context.Context, podName string) (*v1.Pod, error
392392
// of the deployment. If the pod does not exist, the error is ignored.
393393
func (d *Deployment) DeletePod(ctx context.Context, podName string) error {
394394
log := d.deps.Log
395-
ns := d.apiObject.GetNamespace()
395+
ns := d.GetNamespace()
396396
err := k8sutil.RunWithTimeout(ctx, func(ctxChild context.Context) error {
397397
return d.deps.KubeCli.CoreV1().Pods(ns).Delete(ctxChild, podName, meta.DeleteOptions{})
398398
})
@@ -449,7 +449,7 @@ func (d *Deployment) RemovePodFinalizers(ctx context.Context, podName string) er
449449
// of the deployment. If the pvc does not exist, the error is ignored.
450450
func (d *Deployment) DeletePvc(ctx context.Context, pvcName string) error {
451451
log := d.deps.Log
452-
ns := d.apiObject.GetNamespace()
452+
ns := d.GetNamespace()
453453
err := k8sutil.RunWithTimeout(ctx, func(ctxChild context.Context) error {
454454
return d.deps.KubeCli.CoreV1().PersistentVolumeClaims(ns).Delete(ctxChild, pvcName, meta.DeleteOptions{})
455455
})
@@ -482,7 +482,7 @@ func (d *Deployment) UpdatePvc(ctx context.Context, pvc *v1.PersistentVolumeClai
482482
func (d *Deployment) GetOwnedPVCs() ([]v1.PersistentVolumeClaim, error) {
483483
// Get all current PVCs
484484
log := d.deps.Log
485-
pvcs, err := d.deps.KubeCli.CoreV1().PersistentVolumeClaims(d.apiObject.GetNamespace()).List(context.Background(), k8sutil.DeploymentListOpt(d.apiObject.GetName()))
485+
pvcs, err := d.deps.KubeCli.CoreV1().PersistentVolumeClaims(d.GetNamespace()).List(context.Background(), k8sutil.DeploymentListOpt(d.GetName()))
486486
if err != nil {
487487
log.Debug().Err(err).Msg("Failed to list PVCs")
488488
return nil, errors.WithStack(err)
@@ -501,7 +501,7 @@ func (d *Deployment) GetPvc(ctx context.Context, pvcName string) (*v1.Persistent
501501
ctxChild, cancel := context.WithTimeout(ctx, k8sutil.GetRequestTimeout())
502502
defer cancel()
503503

504-
pvc, err := d.deps.KubeCli.CoreV1().PersistentVolumeClaims(d.apiObject.GetNamespace()).Get(ctxChild, pvcName, meta.GetOptions{})
504+
pvc, err := d.deps.KubeCli.CoreV1().PersistentVolumeClaims(d.GetNamespace()).Get(ctxChild, pvcName, meta.GetOptions{})
505505
if err != nil {
506506
log.Debug().Err(err).Str("pvc-name", pvcName).Msg("Failed to get PVC")
507507
return nil, errors.WithStack(err)
@@ -512,9 +512,8 @@ func (d *Deployment) GetPvc(ctx context.Context, pvcName string) (*v1.Persistent
512512
// GetTLSKeyfile returns the keyfile encoded TLS certificate+key for
513513
// the given member.
514514
func (d *Deployment) GetTLSKeyfile(group api.ServerGroup, member api.MemberStatus) (string, error) {
515-
secretName := k8sutil.CreateTLSKeyfileSecretName(d.apiObject.GetName(), group.AsRole(), member.ID)
516-
ns := d.apiObject.GetNamespace()
517-
secrets := d.deps.KubeCli.CoreV1().Secrets(ns)
515+
secretName := k8sutil.CreateTLSKeyfileSecretName(d.GetName(), group.AsRole(), member.ID)
516+
secrets := d.deps.KubeCli.CoreV1().Secrets(d.GetNamespace())
518517
result, err := k8sutil.GetTLSKeyfileSecret(secrets, secretName)
519518
if err != nil {
520519
return "", errors.WithStack(err)
@@ -525,8 +524,8 @@ func (d *Deployment) GetTLSKeyfile(group api.ServerGroup, member api.MemberStatu
525524
// DeleteTLSKeyfile removes the Secret containing the TLS keyfile for the given member.
526525
// If the secret does not exist, the error is ignored.
527526
func (d *Deployment) DeleteTLSKeyfile(ctx context.Context, group api.ServerGroup, member api.MemberStatus) error {
528-
secretName := k8sutil.CreateTLSKeyfileSecretName(d.apiObject.GetName(), group.AsRole(), member.ID)
529-
ns := d.apiObject.GetNamespace()
527+
secretName := k8sutil.CreateTLSKeyfileSecretName(d.GetName(), group.AsRole(), member.ID)
528+
ns := d.GetNamespace()
530529
err := k8sutil.RunWithTimeout(ctx, func(ctxChild context.Context) error {
531530
return d.deps.KubeCli.CoreV1().Secrets(ns).Delete(ctxChild, secretName, meta.DeleteOptions{})
532531
})
@@ -539,7 +538,7 @@ func (d *Deployment) DeleteTLSKeyfile(ctx context.Context, group api.ServerGroup
539538
// DeleteSecret removes the Secret with given name.
540539
// If the secret does not exist, the error is ignored.
541540
func (d *Deployment) DeleteSecret(secretName string) error {
542-
ns := d.apiObject.GetNamespace()
541+
ns := d.GetNamespace()
543542
if err := d.deps.KubeCli.CoreV1().Secrets(ns).Delete(context.Background(), secretName, meta.DeleteOptions{}); err != nil && !k8sutil.IsNotFound(err) {
544543
return errors.WithStack(err)
545544
}
@@ -614,14 +613,14 @@ func (d *Deployment) SecretsInterface() k8sutil.SecretInterface {
614613
}
615614

616615
func (d *Deployment) GetName() string {
617-
return d.apiObject.GetName()
616+
return d.name
618617
}
619618

620619
func (d *Deployment) GetOwnedPods(ctx context.Context) ([]v1.Pod, error) {
621620
ctxChild, cancel := context.WithTimeout(ctx, k8sutil.GetRequestTimeout())
622621
defer cancel()
623622

624-
pods, err := d.GetKubeCli().CoreV1().Pods(d.apiObject.GetNamespace()).List(ctxChild, meta.ListOptions{})
623+
pods, err := d.GetKubeCli().CoreV1().Pods(d.GetNamespace()).List(ctxChild, meta.ListOptions{})
625624
if err != nil {
626625
return nil, err
627626
}

pkg/deployment/deployment.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ const (
107107

108108
// Deployment is the in process state of an ArangoDeployment.
109109
type Deployment struct {
110+
name string
111+
namespace string
112+
110113
apiObject *api.ArangoDeployment // API object
111114
status struct {
112115
mutex sync.Mutex
@@ -143,6 +146,8 @@ func New(config Config, deps Dependencies, apiObject *api.ArangoDeployment) (*De
143146

144147
d := &Deployment{
145148
apiObject: apiObject,
149+
name: apiObject.GetName(),
150+
namespace: apiObject.GetNamespace(),
146151
config: config,
147152
deps: deps,
148153
eventCh: make(chan *deploymentEvent, deploymentEventQueueSize),

pkg/deployment/deployment_inspector.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (d *Deployment) inspectDeployment(lastInterval util.Interval) util.Interval
9999
nextInterval := lastInterval
100100
hasError := false
101101

102-
deploymentName := d.apiObject.GetName()
102+
deploymentName := d.GetName()
103103
defer metrics.SetDuration(inspectDeploymentDurationGauges.WithLabelValues(deploymentName), start)
104104

105105
cachedStatus, err := inspector.NewInspector(d.GetKubeCli(), d.GetMonitoringV1Cli(), d.GetArangoCli(), d.GetNamespace())
@@ -112,7 +112,7 @@ func (d *Deployment) inspectDeployment(lastInterval util.Interval) util.Interval
112112
var updated *api.ArangoDeployment
113113
err = k8sutil.RunWithTimeout(ctxReconciliation, func(ctxChild context.Context) error {
114114
var err error
115-
updated, err = d.deps.DatabaseCRCli.DatabaseV1().ArangoDeployments(d.apiObject.GetNamespace()).Get(ctxChild, deploymentName, metav1.GetOptions{})
115+
updated, err = d.deps.DatabaseCRCli.DatabaseV1().ArangoDeployments(d.GetNamespace()).Get(ctxChild, deploymentName, metav1.GetOptions{})
116116
return err
117117
})
118118
if k8sutil.IsNotFound(err) {

pkg/deployment/deployment_suite_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,8 @@ func createTestDeployment(config Config, arangoDeployment *api.ArangoDeployment)
465465

466466
d := &Deployment{
467467
apiObject: arangoDeployment,
468+
name: arangoDeployment.GetName(),
469+
namespace: arangoDeployment.GetNamespace(),
468470
config: config,
469471
deps: deps,
470472
eventCh: make(chan *deploymentEvent, deploymentEventQueueSize),

pkg/deployment/resources/context.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ type Context interface {
8181
GetMetricsExporterImage() string
8282
// GetArangoImage returns the image name containing the default arango image
8383
GetArangoImage() string
84+
// GetName returns the name of the deployment
85+
GetName() string
8486
// GetNamespace returns the namespace that contains the deployment
8587
GetNamespace() string
8688
// CreateEvent creates a given event.

0 commit comments

Comments
 (0)