This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
forked from 3scale/3scale-operator
/
zync.go
61 lines (52 loc) · 2.02 KB
/
zync.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
package operator
import (
"fmt"
"github.com/3scale/3scale-operator/pkg/3scale/amp/component"
oprand "github.com/3scale/3scale-operator/pkg/crypto/rand"
"k8s.io/apimachinery/pkg/api/errors"
)
func (o *OperatorZyncOptionsProvider) GetZyncOptions() (*component.ZyncOptions, error) {
optProv := component.ZyncOptionsBuilder{}
optProv.AppLabel(*o.APIManagerSpec.AppLabel)
o.setSecretBasedOptions(&optProv)
err := o.setZyncSecretOptions(&optProv)
if err != nil {
return nil, err
}
res, err := optProv.Build()
if err != nil {
return nil, fmt.Errorf("unable to create Zync Options - %s", err)
}
return res, nil
}
func (o *OperatorZyncOptionsProvider) setSecretBasedOptions(zob *component.ZyncOptionsBuilder) error {
err := o.setZyncSecretOptions(zob)
if err != nil {
return fmt.Errorf("unable to create Zync Secret Options - %s", err)
}
return nil
}
func (o *OperatorZyncOptionsProvider) setZyncSecretOptions(zob *component.ZyncOptionsBuilder) error {
defaultZyncSecretKeyBase := oprand.String(16)
defaultZyncDatabasePassword := oprand.String(16)
defaultZyncAuthenticationToken := oprand.String(16)
currSecret, err := getSecret(component.ZyncSecretName, o.Namespace, o.Client)
if err != nil {
if errors.IsNotFound(err) {
// Set options defaults
zob.SecretKeyBase(defaultZyncSecretKeyBase)
zob.DatabasePassword(defaultZyncDatabasePassword)
zob.AuthenticationToken(defaultZyncAuthenticationToken)
} else {
return err
}
} else {
// If a field of a secret already exists in the deployed secret then
// We do not modify it. Otherwise we set a default value
secretData := currSecret.Data
zob.SecretKeyBase(getSecretDataValueOrDefault(secretData, component.ZyncSecretKeyBaseFieldName, defaultZyncSecretKeyBase))
zob.DatabasePassword(getSecretDataValueOrDefault(secretData, component.ZyncSecretDatabasePasswordFieldName, defaultZyncDatabasePassword))
zob.AuthenticationToken(getSecretDataValueOrDefault(secretData, component.ZyncSecretAuthenticationTokenFieldName, defaultZyncAuthenticationToken))
}
return nil
}