Skip to content

Commit 5652474

Browse files
aalexandruaalexandruradu-catalina
authored
New Feature: Service metadata (#77)
* Add service metadata endpoints * New release 1.5.0-unstable (#70) Co-authored-by: aalexandru <[email protected]> * Add chargebackBusinessUnit field; Remove k8sInfraRelease field (#74) * Add chargebackBusinessUnit field; Remove k8sInfraRelease field * Fix go deps * go mod tidy * Updated controller-gen version * Fix go.sum imports * Bump go version to 1.21 * Refactor deprecated clientConfig * Refactor clientConfig webhook configuration * Fix G304 (CWE-22) * Add missing license * Update makefile K8S_VERSION to 1.25.0 * Update controller-gen crd options --------- Co-authored-by: aalexandru <[email protected]> * Add recommended helm labels (#71) * Add recommended helm labels * Increase chart version * New release 1.4.2 (#75) Co-authored-by: aalexandru <[email protected]> * Fix leader election (#76) Co-authored-by: aalexandru <[email protected]> * Update kind version * Add new crd & controller reconciliation logic * Updated serviceMetadata patching logic * make swagger * Add chargebackBusinessUnit field; Remove k8sInfraRelease field (#74) * Add chargebackBusinessUnit field; Remove k8sInfraRelease field * Fix go deps * go mod tidy * Updated controller-gen version * Fix go.sum imports * Bump go version to 1.21 * Refactor deprecated clientConfig * Refactor clientConfig webhook configuration * Fix G304 (CWE-22) * Add missing license * Update makefile K8S_VERSION to 1.25.0 * Update controller-gen crd options --------- Co-authored-by: aalexandru <[email protected]> * Update kind version * Add new crd & controller reconciliation logic * Updated serviceMetadata patching logic * make swagger * Add client config defaults * Updated service metadata patching logic * Ignore service object delete events * Fix linting errors * Add missing license --------- Co-authored-by: aalexandru <[email protected]> Co-authored-by: caradu <[email protected]>
1 parent 43687cd commit 5652474

36 files changed

+1800
-94
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ kustomize: ## Download kustomize locally if necessary.
217217
@[ -f $(KUSTOMIZE) ] || GOBIN=$(shell pwd)/bin go install sigs.k8s.io/kustomize/kustomize/[email protected]
218218

219219
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
220-
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=$(MANAGER_ROLE) webhook paths="$(shell pwd)/pkg/api/..." output:crd:artifacts:config=$(shell pwd)/config/crd/bases output:rbac:artifacts:config=$(shell pwd)/config/rbac
220+
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=$(MANAGER_ROLE) webhook paths="$(shell pwd)/pkg/..." output:crd:artifacts:config=$(shell pwd)/config/crd/bases output:rbac:artifacts:config=$(shell pwd)/config/rbac
221221

222222
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
223223
$(CONTROLLER_GEN) object:headerFile="$(shell pwd)/hack/boilerplate.go.txt" paths="$(shell pwd)/pkg/api/..."
@@ -229,5 +229,5 @@ generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and
229229

230230
SWAGGER_CLI = $(shell pwd)/bin/swag
231231
swagger:
232-
@[ -f $(SWAGGER_CLI) ] || GOBIN=$(shell pwd)/bin go install github.com/swaggo/swag/cmd/swag@v1.8.12
232+
@[ -f $(SWAGGER_CLI) ] || GOBIN=$(shell pwd)/bin go install github.com/swaggo/swag/cmd/swag@v1.16.2
233233
$(SWAGGER_CLI) init --parseDependency --parseInternal --parseDepth 2 -g cmd/apiserver/apiserver.go --output pkg/apiserver/docs/

cmd/apiserver/apiserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var Version = "dev"
3535
// @version 1.0
3636
// @description Cluster Registry API
3737

38-
// @host http://127.0.0.1:8080
38+
// @host 127.0.0.1:8080
3939
// @BasePath /api
4040

4141
// @schemes http https

cmd/client/client.go

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,17 @@ governing permissions and limitations under the License.
1313
package main
1414

1515
import (
16+
"encoding/base64"
1617
"flag"
17-
"k8s.io/client-go/tools/leaderelection/resourcelock"
18-
"os"
19-
"sigs.k8s.io/controller-runtime/pkg/cache"
20-
18+
registryv1alpha1 "github.com/adobe/cluster-registry/pkg/api/registry/v1alpha1"
2119
"github.com/adobe/cluster-registry/pkg/client/controllers"
2220
"github.com/adobe/cluster-registry/pkg/config"
2321
monitoring "github.com/adobe/cluster-registry/pkg/monitoring/client"
2422
"github.com/adobe/cluster-registry/pkg/sqs"
25-
2623
"github.com/prometheus/client_golang/prometheus/promhttp"
27-
28-
"encoding/base64"
24+
"k8s.io/apimachinery/pkg/runtime/schema"
25+
"k8s.io/client-go/tools/leaderelection/resourcelock"
26+
"os"
2927

3028
configv1 "github.com/adobe/cluster-registry/pkg/api/config/v1"
3129
registryv1 "github.com/adobe/cluster-registry/pkg/api/registry/v1"
@@ -48,10 +46,13 @@ var (
4846
func init() {
4947
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
5048
utilruntime.Must(registryv1.AddToScheme(scheme))
49+
utilruntime.Must(registryv1alpha1.AddToScheme(scheme))
5150
utilruntime.Must(configv1.AddToScheme(scheme))
5251
}
5352

5453
func main() {
54+
ctx := ctrl.SetupSignalHandler()
55+
5556
var configFile string
5657
var metricsAddr string
5758
var enableLeaderElection bool
@@ -80,18 +81,20 @@ func main() {
8081
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
8182

8283
var err error
83-
clientConfig := configv1.ClientConfig{
84+
var clientConfig configv1.ClientConfig
85+
clientConfigDefaults := configv1.ClientConfig{
8486
Namespace: namespace,
8587
AlertmanagerWebhook: configv1.AlertmanagerWebhookConfig{
8688
BindAddress: alertmanagerWebhookAddr,
8789
AlertMap: []configv1.AlertRule{},
8890
},
91+
ServiceMetadata: configv1.ServiceMetadataConfig{
92+
WatchedGVKs: []configv1.WatchedGVK{},
93+
ServiceIdAnnotation: "adobe.serviceid",
94+
},
8995
}
9096
options := ctrl.Options{
91-
Scheme: scheme,
92-
Cache: cache.Options{
93-
Namespaces: []string{namespace},
94-
},
97+
Scheme: scheme,
9598
MetricsBindAddress: metricsAddr,
9699
HealthProbeBindAddress: probeAddr,
97100
LeaderElection: enableLeaderElection,
@@ -100,7 +103,7 @@ func main() {
100103
}
101104

102105
if configFile != "" {
103-
options, clientConfig, err = apply(configFile)
106+
options, clientConfig, err = apply(configFile, &clientConfigDefaults)
104107
if err != nil {
105108
setupLog.Error(err, "unable to load the config file")
106109
os.Exit(1)
@@ -144,6 +147,27 @@ func main() {
144147
os.Exit(1)
145148
}
146149

150+
if err = (&controllers.ServiceMetadataWatcherReconciler{
151+
Client: mgr.GetClient(),
152+
Log: ctrl.Log.WithName("controllers").WithName("ServiceMetadataWatcher"),
153+
Scheme: mgr.GetScheme(),
154+
WatchedGVKs: func(cfg configv1.ClientConfig) []schema.GroupVersionKind {
155+
var GVKs []schema.GroupVersionKind
156+
for _, gvk := range cfg.ServiceMetadata.WatchedGVKs {
157+
GVKs = append(GVKs, schema.GroupVersionKind{
158+
Group: gvk.Group,
159+
Version: gvk.Version,
160+
Kind: gvk.Kind,
161+
})
162+
}
163+
return GVKs
164+
}(clientConfig),
165+
ServiceIdAnnotation: clientConfig.ServiceMetadata.ServiceIdAnnotation,
166+
}).SetupWithManager(ctx, mgr); err != nil {
167+
setupLog.Error(err, "unable to create controller", "controller", "ServiceMetadataWatcher")
168+
os.Exit(1)
169+
}
170+
147171
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
148172
setupLog.Error(err, "unable to set up health check")
149173
os.Exit(1)
@@ -174,14 +198,14 @@ func main() {
174198
}()
175199

176200
setupLog.Info("starting cluster-registry-client")
177-
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
201+
if err := mgr.Start(ctx); err != nil {
178202
setupLog.Error(err, "problem running cluster-registry-client")
179203
os.Exit(1)
180204
}
181205
}
182206

183-
func apply(configFile string) (ctrl.Options, configv1.ClientConfig, error) {
184-
options, cfg, err := configv1.Load(scheme, configFile)
207+
func apply(configFile string, clientConfigDefaults *configv1.ClientConfig) (ctrl.Options, configv1.ClientConfig, error) {
208+
options, cfg, err := configv1.Load(scheme, configFile, clientConfigDefaults)
185209
if err != nil {
186210
return options, cfg, err
187211
}

config/crd/bases/registry.ethos.adobe.com_clusters.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,15 @@ spec:
204204
registeredAt:
205205
description: Timestamp when cluster was registered in Cluster Registry
206206
type: string
207+
services:
208+
additionalProperties:
209+
additionalProperties:
210+
additionalProperties:
211+
type: string
212+
type: object
213+
type: object
214+
description: ServiceMetadata service specific metadata
215+
type: object
207216
shortName:
208217
description: Cluster name, without dash
209218
maxLength: 64
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
apiVersion: apiextensions.k8s.io/v1
3+
kind: CustomResourceDefinition
4+
metadata:
5+
annotations:
6+
controller-gen.kubebuilder.io/version: v0.13.0
7+
name: servicemetadatawatchers.registry.ethos.adobe.com
8+
spec:
9+
group: registry.ethos.adobe.com
10+
names:
11+
kind: ServiceMetadataWatcher
12+
listKind: ServiceMetadataWatcherList
13+
plural: servicemetadatawatchers
14+
singular: servicemetadatawatcher
15+
scope: Namespaced
16+
versions:
17+
- name: v1alpha1
18+
schema:
19+
openAPIV3Schema:
20+
description: ServiceMetadataWatcher is the Schema for the servicemetadatawatchers
21+
API
22+
properties:
23+
apiVersion:
24+
description: 'APIVersion defines the versioned schema of this representation
25+
of an object. Servers should convert recognized schemas to the latest
26+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
27+
type: string
28+
kind:
29+
description: 'Kind is a string value representing the REST resource this
30+
object represents. Servers may infer this from the endpoint the client
31+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
32+
type: string
33+
metadata:
34+
type: object
35+
spec:
36+
description: ServiceMetadataWatcherSpec defines the desired state of ServiceMetadataWatcher
37+
properties:
38+
watchedServiceObjects:
39+
items:
40+
properties:
41+
objectReference:
42+
properties:
43+
apiVersion:
44+
type: string
45+
kind:
46+
type: string
47+
name:
48+
type: string
49+
required:
50+
- apiVersion
51+
- kind
52+
- name
53+
type: object
54+
watchedFields:
55+
items:
56+
properties:
57+
dst:
58+
type: string
59+
src:
60+
type: string
61+
required:
62+
- dst
63+
- src
64+
type: object
65+
type: array
66+
required:
67+
- objectReference
68+
- watchedFields
69+
type: object
70+
type: array
71+
required:
72+
- watchedServiceObjects
73+
type: object
74+
status:
75+
description: ServiceMetadataWatcherStatus defines the observed state of
76+
ServiceMetadataWatcher
77+
type: object
78+
type: object
79+
served: true
80+
storage: true
81+
subresources:
82+
status: {}

config/crd/kustomization.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
# It should be run by config/default
44
resources:
55
- bases/registry.ethos.adobe.com_clusters.yaml
6+
- bases/registry.ethos.adobe.com.servicemetadatawatchers.yaml
67
#+kubebuilder:scaffold:crdkustomizeresource
78

89
patchesStrategicMerge:
910
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix.
1011
# patches here are for enabling the conversion webhook for each CRD
1112
#- patches/webhook_in_clusters.yaml
13+
#- path: patches/webhook_in_servicemetadatawatchers.yaml
1214
#+kubebuilder:scaffold:crdkustomizewebhookpatch
1315

1416
# [CERTMANAGER] To enable webhook, uncomment all the sections with [CERTMANAGER] prefix.
1517
# patches here are for enabling the CA injection for each CRD
1618
#- patches/cainjection_in_clusters.yaml
19+
#- path: patches/cainjection_in_servicemetadatawatchers.yaml
1720
#+kubebuilder:scaffold:crdkustomizecainjectionpatch
1821

1922
# the following config is for teaching kustomize how to do kustomization for CRDs.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# The following patch adds a directive for certmanager to inject CA into the CRD
2+
apiVersion: apiextensions.k8s.io/v1
3+
kind: CustomResourceDefinition
4+
metadata:
5+
annotations:
6+
cert-manager.io/inject-ca-from: CERTIFICATE_NAMESPACE/CERTIFICATE_NAME
7+
name: servicemetadatawatchers.registry.ethos.adobe.com
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# The following patch enables a conversion webhook for the CRD
2+
apiVersion: apiextensions.k8s.io/v1
3+
kind: CustomResourceDefinition
4+
metadata:
5+
name: servicemetadatawatchers.registry.ethos.adobe.com
6+
spec:
7+
conversion:
8+
strategy: Webhook
9+
webhook:
10+
clientConfig:
11+
service:
12+
namespace: system
13+
name: webhook-service
14+
path: /convert
15+
conversionReviewVersions:
16+
- v1

config/rbac/role.yaml

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
21
---
32
apiVersion: rbac.authorization.k8s.io/v1
43
kind: ClusterRole
54
metadata:
6-
creationTimestamp: null
75
name: cluster-registry
86
rules:
97
- apiGroups:
@@ -32,3 +30,29 @@ rules:
3230
- get
3331
- patch
3432
- update
33+
- apiGroups:
34+
- registry.ethos.adobe.com
35+
resources:
36+
- servicemetadatawatchers
37+
verbs:
38+
- create
39+
- delete
40+
- get
41+
- list
42+
- patch
43+
- update
44+
- watch
45+
- apiGroups:
46+
- registry.ethos.adobe.com
47+
resources:
48+
- servicemetadatawatchers/finalizers
49+
verbs:
50+
- update
51+
- apiGroups:
52+
- registry.ethos.adobe.com
53+
resources:
54+
- servicemetadatawatchers/status
55+
verbs:
56+
- get
57+
- patch
58+
- update
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# permissions for end users to edit servicemetadatawatchers.
2+
apiVersion: rbac.authorization.k8s.io/v1
3+
kind: ClusterRole
4+
metadata:
5+
labels:
6+
app.kubernetes.io/name: clusterrole
7+
app.kubernetes.io/instance: servicemetadatawatcher-editor-role
8+
app.kubernetes.io/component: rbac
9+
app.kubernetes.io/created-by: cluster-registry
10+
app.kubernetes.io/part-of: cluster-registry
11+
app.kubernetes.io/managed-by: kustomize
12+
name: servicemetadatawatcher-editor-role
13+
rules:
14+
- apiGroups:
15+
- registry.ethos.adobe.com
16+
resources:
17+
- servicemetadatawatchers
18+
verbs:
19+
- create
20+
- delete
21+
- get
22+
- list
23+
- patch
24+
- update
25+
- watch
26+
- apiGroups:
27+
- registry.ethos.adobe.com
28+
resources:
29+
- servicemetadatawatchers/status
30+
verbs:
31+
- get

0 commit comments

Comments
 (0)