Skip to content

Commit de48f79

Browse files
committed
[WIP] Add initial watcher api conf generation
1 parent aa728c6 commit de48f79

File tree

16 files changed

+278
-10
lines changed

16 files changed

+278
-10
lines changed

api/bases/watcher.openstack.org_watcherapis.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ spec:
6464
secret:
6565
description: Secret containing all passwords / keys needed
6666
type: string
67+
serviceUser:
68+
default: watcher
69+
description: |-
70+
ServiceUser - optional username used for this service to register in
71+
keystone
72+
type: string
6773
required:
6874
- databaseInstance
6975
- secret

api/bases/watcher.openstack.org_watchers.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ spec:
7171
default: osp-secret
7272
description: Secret containing all passwords / keys needed
7373
type: string
74+
serviceUser:
75+
default: watcher
76+
description: |-
77+
ServiceUser - optional username used for this service to register in
78+
keystone
79+
type: string
7480
required:
7581
- databaseInstance
7682
- rabbitMqClusterName

api/v1beta1/common_types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ type WatcherCommon struct {
3232
// +kubebuilder:default=watcher
3333
// DatabaseAccount - MariaDBAccount CR name used for watcher DB, defaults to watcher
3434
DatabaseAccount string `json:"databaseAccount"`
35+
36+
// +kubebuilder:validation:Optional
37+
// +kubebuilder:default=watcher
38+
// ServiceUser - optional username used for this service to register in
39+
// keystone
40+
ServiceUser string `json:"serviceUser"`
3541
}
3642

3743
// WatcherTemplate defines the fields used in the top level CR

config/crd/bases/watcher.openstack.org_watcherapis.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ spec:
6464
secret:
6565
description: Secret containing all passwords / keys needed
6666
type: string
67+
serviceUser:
68+
default: watcher
69+
description: |-
70+
ServiceUser - optional username used for this service to register in
71+
keystone
72+
type: string
6773
required:
6874
- databaseInstance
6975
- secret

config/crd/bases/watcher.openstack.org_watchers.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ spec:
7171
default: osp-secret
7272
description: Secret containing all passwords / keys needed
7373
type: string
74+
serviceUser:
75+
default: watcher
76+
description: |-
77+
ServiceUser - optional username used for this service to register in
78+
keystone
79+
type: string
7480
required:
7581
- databaseInstance
7682
- rabbitMqClusterName

config/rbac/role.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,38 @@ rules:
2828
- patch
2929
- update
3030
- watch
31+
- apiGroups:
32+
- keystone.openstack.org
33+
resources:
34+
- keystoneapis
35+
verbs:
36+
- get
37+
- list
38+
- watch
39+
- apiGroups:
40+
- keystone.openstack.org
41+
resources:
42+
- keystoneendpoints
43+
verbs:
44+
- create
45+
- delete
46+
- get
47+
- list
48+
- patch
49+
- update
50+
- watch
51+
- apiGroups:
52+
- keystone.openstack.org
53+
resources:
54+
- keystoneservices
55+
verbs:
56+
- create
57+
- delete
58+
- get
59+
- list
60+
- patch
61+
- update
62+
- watch
3163
- apiGroups:
3264
- mariadb.openstack.org
3365
resources:

controllers/watcher_common.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import (
1616
"sigs.k8s.io/controller-runtime/pkg/log"
1717

1818
"github.com/openstack-k8s-operators/lib-common/modules/common/condition"
19+
"github.com/openstack-k8s-operators/lib-common/modules/common/env"
20+
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
21+
"github.com/openstack-k8s-operators/lib-common/modules/common/secret"
1922
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
2023
)
2124

@@ -188,3 +191,38 @@ func ensureSecret(
188191

189192
return hash, ctrl.Result{}, *secret, nil
190193
}
194+
195+
func GenerateConfigsGeneric(
196+
ctx context.Context, helper *helper.Helper,
197+
instance client.Object,
198+
envVars *map[string]env.Setter,
199+
templateParameters map[string]interface{},
200+
customData map[string]string,
201+
cmLabels map[string]string,
202+
scripts bool,
203+
) error {
204+
205+
cms := []util.Template{
206+
// Templates where the watcher config is stored
207+
{
208+
Name: fmt.Sprintf("%s-config-data", instance.GetName()),
209+
Namespace: instance.GetNamespace(),
210+
Type: util.TemplateTypeConfig,
211+
InstanceType: instance.GetObjectKind().GroupVersionKind().Kind,
212+
ConfigOptions: templateParameters,
213+
CustomData: customData,
214+
Labels: cmLabels,
215+
},
216+
}
217+
if scripts {
218+
cms = append(cms, util.Template{
219+
Name: fmt.Sprintf("%s-scripts", instance.GetName()),
220+
Namespace: instance.GetNamespace(),
221+
Type: util.TemplateTypeScripts,
222+
InstanceType: instance.GetObjectKind().GroupVersionKind().Kind,
223+
ConfigOptions: templateParameters,
224+
Labels: cmLabels,
225+
})
226+
}
227+
return secret.EnsureSecrets(ctx, helper, instance, cms, envVars)
228+
}

controllers/watcherapi_controller.go

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ import (
3030
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3131

3232
"github.com/go-logr/logr"
33+
keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1"
3334
"github.com/openstack-k8s-operators/lib-common/modules/common/condition"
35+
"github.com/openstack-k8s-operators/lib-common/modules/common/endpoint"
3436
"github.com/openstack-k8s-operators/lib-common/modules/common/env"
3537
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
38+
"github.com/openstack-k8s-operators/lib-common/modules/common/labels"
39+
"github.com/openstack-k8s-operators/lib-common/modules/common/service"
3640
mariadbv1 "github.com/openstack-k8s-operators/mariadb-operator/api/v1beta1"
3741

3842
watcherv1beta1 "github.com/openstack-k8s-operators/watcher-operator/api/v1beta1"
@@ -59,6 +63,9 @@ func (r *WatcherAPIReconciler) GetLogger(ctx context.Context) logr.Logger {
5963
//+kubebuilder:rbac:groups=watcher.openstack.org,resources=watcherapis/status,verbs=get;update;patch
6064
//+kubebuilder:rbac:groups=watcher.openstack.org,resources=watcherapis/finalizers,verbs=update
6165
//+kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch;create;update;patch;delete;
66+
//+kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneapis,verbs=get;list;watch;
67+
//+kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneservices,verbs=get;list;watch;create;update;patch;delete;
68+
//+kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneendpoints,verbs=get;list;watch;create;update;patch;delete;
6269

6370
// Reconcile is part of the main kubernetes reconciliation loop which aims to
6471
// move the current state of the cluster closer to the desired state.
@@ -181,9 +188,6 @@ func (r *WatcherAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request)
181188
}
182189

183190
// generateServiceConfigs - create Secret which holds the service configuration
184-
// NOTE - jgilaber this function is WIP, currently implements a fraction of its
185-
// functionality and will be expanded of further iteration to actually generate
186-
// the service configs
187191
func (r *WatcherAPIReconciler) generateServiceConfigs(
188192
ctx context.Context, instance *watcherv1beta1.WatcherAPI,
189193
secret corev1.Secret, db *mariadbv1.Database,
@@ -192,14 +196,58 @@ func (r *WatcherAPIReconciler) generateServiceConfigs(
192196
Log := r.GetLogger(ctx)
193197
Log.Info("generateServiceConfigs - reconciling")
194198

195-
// replace by actual usage in future iterations
196-
_ = db
197-
_ = helper
198-
_ = instance
199-
_ = secret
200-
_ = envVars
199+
labels := labels.GetLabels(instance, labels.GetGroupLabel(watcher.ServiceName), map[string]string{})
200+
// jgilaber this might be wrong? we should probably get keystonapi in the
201+
// watcher controller and set the url in the spec eventually?
202+
keystoneAPI, err := keystonev1.GetKeystoneAPI(ctx, helper, instance.Namespace, map[string]string{})
203+
// KeystoneAPI not available we should not aggregate the error and continue
204+
if err != nil {
205+
instance.Status.Conditions.Set(condition.FalseCondition(
206+
condition.ServiceConfigReadyCondition,
207+
condition.ErrorReason,
208+
condition.SeverityWarning,
209+
condition.ServiceConfigReadyErrorMessage,
210+
"keystoneAPI not found"))
211+
return err
212+
}
213+
keystoneInternalURL, err := keystoneAPI.GetEndpoint(endpoint.EndpointInternal)
214+
if err != nil {
215+
return err
216+
}
217+
// customData hold any customization for the service.
218+
// NOTE jgilaber making an empty map for now, we'll probably want to
219+
// implement CustomServiceConfig later
220+
customData := map[string]string{}
221+
222+
databaseAccount := db.GetAccount()
223+
databaseSecret := db.GetSecret()
224+
templateParameters := map[string]interface{}{
225+
"DatabaseConnection": fmt.Sprintf("mysql+pymysql://%s:%s@%s/%s?charset=utf8&plugin=dbcounter",
226+
databaseAccount.Spec.UserName,
227+
string(databaseSecret.Data[mariadbv1.DatabasePasswordSelector]),
228+
db.GetDatabaseHostname(),
229+
watcher.DatabaseName,
230+
),
231+
"KeystoneAuthURL": keystoneInternalURL,
232+
"ServicePassword": string(secret.Data[instance.Spec.PasswordSelectors.Service]),
233+
"ServiceUser": instance.Spec.ServiceUser,
234+
"TransportURL": "", // TODO jgilaber implement getting this URL once we
235+
// have rabbitmq support added to the Watcher controller
236+
"MemcachedServers": "", // TODO jgilaber implement getting this URL once we
237+
// have memchache support
238+
}
201239

202-
return nil
240+
// create httpd vhost template parameters
241+
httpdVhostConfig := map[string]interface{}{}
242+
for _, endpt := range []service.Endpoint{service.EndpointInternal, service.EndpointPublic} {
243+
endptConfig := map[string]interface{}{}
244+
endptConfig["ServerName"] = fmt.Sprintf("%s-%s.%s.svc", watcher.ServiceName, endpt.String(), instance.Namespace)
245+
endptConfig["TLS"] = false // default TLS to false, and set it below when implemented
246+
httpdVhostConfig[endpt.String()] = endptConfig
247+
}
248+
templateParameters["VHosts"] = httpdVhostConfig
249+
250+
return GenerateConfigsGeneric(ctx, helper, instance, envVars, templateParameters, customData, labels, false)
203251
}
204252

205253
func (r *WatcherAPIReconciler) reconcileDelete(ctx context.Context, instance *watcherv1beta1.WatcherAPI, helper *helper.Helper) (ctrl.Result, error) {

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/onsi/ginkgo/v2 v2.20.1
99
github.com/onsi/gomega v1.34.1
1010
github.com/openstack-k8s-operators/infra-operator/apis v0.5.0
11+
github.com/openstack-k8s-operators/keystone-operator/api v0.5.0
1112
github.com/openstack-k8s-operators/lib-common/modules/common v0.5.0
1213
github.com/openstack-k8s-operators/lib-common/modules/test v0.5.0
1314
github.com/openstack-k8s-operators/mariadb-operator/api v0.5.0
@@ -38,6 +39,7 @@ require (
3839
github.com/google/go-cmp v0.6.0 // indirect
3940
github.com/google/gofuzz v1.2.0 // indirect
4041
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 // indirect
42+
github.com/gophercloud/gophercloud v1.14.1 // indirect
4143
github.com/imdario/mergo v0.3.16 // indirect
4244
github.com/josharian/intern v1.0.0 // indirect
4345
github.com/json-iterator/go v1.1.12 // indirect
@@ -47,6 +49,7 @@ require (
4749
github.com/modern-go/reflect2 v1.0.2 // indirect
4850
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
4951
github.com/openshift/api v3.9.0+incompatible // indirect
52+
github.com/openstack-k8s-operators/lib-common/modules/openstack v0.4.1-0.20241014140317-e5c35d28f3af // indirect
5053
github.com/pkg/errors v0.9.1 // indirect
5154
github.com/prometheus/client_golang v1.18.0 // indirect
5255
github.com/prometheus/client_model v0.5.0 // indirect

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 h1:FKHo8hFI3A+7w0aUQu
4646
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo=
4747
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
4848
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
49+
github.com/gophercloud/gophercloud v1.14.1 h1:DTCNaTVGl8/cFu58O1JwWgis9gtISAFONqpMKNg/Vpw=
50+
github.com/gophercloud/gophercloud v1.14.1/go.mod h1:aAVqcocTSXh2vYFZ1JTvx4EQmfgzxRcNupUfxZbBNDM=
4951
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
5052
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
5153
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
@@ -77,8 +79,12 @@ github.com/openshift/api v0.0.0-20240830023148-b7d0481c9094 h1:J1wuGhVxpsHykZBa6
7779
github.com/openshift/api v0.0.0-20240830023148-b7d0481c9094/go.mod h1:CxgbWAlvu2iQB0UmKTtRu1YfepRg1/vJ64n2DlIEVz4=
7880
github.com/openstack-k8s-operators/infra-operator/apis v0.5.0 h1:+1Q1Ux7DeEg3dPsVEWsm+MCJASlAy9FH/CGRD5jZeXo=
7981
github.com/openstack-k8s-operators/infra-operator/apis v0.5.0/go.mod h1:J9oUh3eGBvAFfyUMiPxPRBSxAcO8rnwITN4RTh/It+8=
82+
github.com/openstack-k8s-operators/keystone-operator/api v0.5.0 h1:h/Ce2OjdNrkDh/rJuZPdOsxrsm2uC+E57Mmf34oyWR0=
83+
github.com/openstack-k8s-operators/keystone-operator/api v0.5.0/go.mod h1:saoorrsPo3DzDPGM6PJ8sQJBNuNRGCHjRHChRQmkoQ0=
8084
github.com/openstack-k8s-operators/lib-common/modules/common v0.5.0 h1:wto7Vprhr84z2LJzjbbw589MGkfjKtpHnhIhzgOa+BI=
8185
github.com/openstack-k8s-operators/lib-common/modules/common v0.5.0/go.mod h1:tNeup9Xl7j2eaeMslJ/rt59NNEAw7ATf6RuebS/YkSk=
86+
github.com/openstack-k8s-operators/lib-common/modules/openstack v0.4.1-0.20241014140317-e5c35d28f3af h1:fevDUHmqcnI4wDTKupKe/CcgVdgNpZXWkJx8u0/xEXs=
87+
github.com/openstack-k8s-operators/lib-common/modules/openstack v0.4.1-0.20241014140317-e5c35d28f3af/go.mod h1:djfljx3jfHqywhY3oDvPg/GLKwiFVkds6v7P7/Yg+8g=
8288
github.com/openstack-k8s-operators/lib-common/modules/test v0.5.0 h1:rUVJUKFWQuXYQ3LPNs7wIJdka5EqUmLMT3RRpWkuqRo=
8389
github.com/openstack-k8s-operators/lib-common/modules/test v0.5.0/go.mod h1:LV0jo5etIsGyINpmB37i4oWR8zU6ApIuh7fsqGGA41o=
8490
github.com/openstack-k8s-operators/mariadb-operator/api v0.5.0 h1:XBx1TuyKhgtWAigYVcdqTUzIwWRYHN63pfa0zxHB12M=
@@ -116,6 +122,7 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
116122
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
117123
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
118124
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
125+
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
119126
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
120127
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
121128
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
@@ -128,6 +135,7 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
128135
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
129136
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
130137
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
138+
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
131139
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
132140
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
133141
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
@@ -141,6 +149,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
141149
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
142150
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
143151
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
152+
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
144153
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
145154
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
146155
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -152,6 +161,7 @@ golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
152161
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
153162
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
154163
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
164+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
155165
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
156166
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
157167
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=

0 commit comments

Comments
 (0)