Skip to content

Commit e7fe0ad

Browse files
committed
feat: support custom labels and annotations on generated secrets
1 parent e994541 commit e7fe0ad

File tree

7 files changed

+134
-13
lines changed

7 files changed

+134
-13
lines changed

api/v1/clustertoken_types.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type ClusterTokenSpec struct {
3333
// Important: Run "make" to regenerate code after modifying this file
3434

3535
// +kubebuilder:validation:Required
36-
Secret clusterTokenSecretSpec `json:"secret"`
36+
Secret ClusterTokenSecretSpec `json:"secret"`
3737

3838
// +optional
3939
// +kubebuilder:example:="123456789"
@@ -63,7 +63,7 @@ type ClusterTokenSpec struct {
6363
RepositoryIDs []int64 `json:"repositoryIDs,omitempty"`
6464
}
6565

66-
type clusterTokenSecretSpec struct {
66+
type ClusterTokenSecretSpec struct {
6767
// +kubebuilder:validation:Required
6868
// +kubebuilder:validation:MaxLength:=253
6969
// +kubebuilder:example:="default"
@@ -75,6 +75,14 @@ type clusterTokenSecretSpec struct {
7575
// Name for the Secret managed by this ClusterToken (defaults to the name of the ClusterToken)
7676
Name string `json:"name,omitempty"`
7777

78+
// +optional
79+
// Extra labels for the Secret managed by this Token
80+
Labels map[string]string `json:"labels,omitempty"`
81+
82+
// +optional
83+
// Extra annotations for the Secret managed by this Token
84+
Annotations map[string]string `json:"annotations,omitempty"`
85+
7886
// +optional
7987
// Create a secret with 'username' and 'password' fields for HTTP Basic Auth rather than simply 'token'
8088
BasicAuth bool `json:"basicAuth,omitempty"`
@@ -133,6 +141,14 @@ func (t *ClusterToken) GetSecretName() string {
133141
return secretName
134142
}
135143

144+
func (t *ClusterToken) GetSecretLabels() map[string]string {
145+
return t.Spec.Secret.Labels
146+
}
147+
148+
func (t *ClusterToken) GetSecretAnnotations() map[string]string {
149+
return t.Spec.Secret.Annotations
150+
}
151+
136152
func (t *ClusterToken) GetSecretBasicAuth() bool {
137153
return t.Spec.Secret.BasicAuth
138154
}

api/v1/token_types.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type TokenSpec struct {
3434

3535
// +optional
3636
// Override the default token secret name and type
37-
Secret tokenSecretSpec `json:"secret,omitempty"`
37+
Secret TokenSecretSpec `json:"secret,omitempty"`
3838

3939
// +optional
4040
// +kubebuilder:example:="123456789"
@@ -64,12 +64,20 @@ type TokenSpec struct {
6464
RepositoryIDs []int64 `json:"repositoryIDs,omitempty"`
6565
}
6666

67-
type tokenSecretSpec struct {
67+
type TokenSecretSpec struct {
6868
// +optional
6969
// +kubebuilder:validation:MaxLength:=253
70-
// Name for the Secret managed by this ClusterToken (defaults to the name of the Token)
70+
// Name for the Secret managed by this Token (defaults to the name of the Token)
7171
Name string `json:"name,omitempty"`
7272

73+
// +optional
74+
// Extra labels for the Secret managed by this Token
75+
Labels map[string]string `json:"labels,omitempty"`
76+
77+
// +optional
78+
// Extra annotations for the Secret managed by this Token
79+
Annotations map[string]string `json:"annotations,omitempty"`
80+
7381
// +optional
7482
// Create a secret with 'username' and 'password' fields for HTTP Basic Auth rather than simply 'token'
7583
BasicAuth bool `json:"basicAuth,omitempty"`
@@ -126,6 +134,14 @@ func (t *Token) GetSecretName() string {
126134
return secretName
127135
}
128136

137+
func (t *Token) GetSecretLabels() map[string]string {
138+
return t.Spec.Secret.Labels
139+
}
140+
141+
func (t *Token) GetSecretAnnotations() map[string]string {
142+
return t.Spec.Secret.Annotations
143+
}
144+
129145
func (t *Token) GetSecretBasicAuth() bool {
130146
return t.Spec.Secret.BasicAuth
131147
}

api/v1/zz_generated.deepcopy.go

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

config/crd/bases/github.as-code.io_clustertokens.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,23 @@ spec:
260260
type: array
261261
secret:
262262
properties:
263+
annotations:
264+
additionalProperties:
265+
type: string
266+
description:
267+
Extra annotations for the Secret managed by this
268+
Token
269+
type: object
263270
basicAuth:
264271
description:
265272
Create a secret with 'username' and 'password' fields
266273
for HTTP Basic Auth rather than simply 'token'
267274
type: boolean
275+
labels:
276+
additionalProperties:
277+
type: string
278+
description: Extra labels for the Secret managed by this Token
279+
type: object
268280
name:
269281
description:
270282
Name for the Secret managed by this ClusterToken

config/crd/bases/github.as-code.io_tokens.yaml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,15 +261,27 @@ spec:
261261
secret:
262262
description: Override the default token secret name and type
263263
properties:
264+
annotations:
265+
additionalProperties:
266+
type: string
267+
description:
268+
Extra annotations for the Secret managed by this
269+
Token
270+
type: object
264271
basicAuth:
265272
description:
266273
Create a secret with 'username' and 'password' fields
267274
for HTTP Basic Auth rather than simply 'token'
268275
type: boolean
276+
labels:
277+
additionalProperties:
278+
type: string
279+
description: Extra labels for the Secret managed by this Token
280+
type: object
269281
name:
270282
description:
271-
Name for the Secret managed by this ClusterToken
272-
(defaults to the name of the Token)
283+
Name for the Secret managed by this Token (defaults
284+
to the name of the Token)
273285
maxLength: 253
274286
type: string
275287
type: object

internal/tokenmanager/token_manager.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type tokenManager interface {
2525
GetRefreshInterval() time.Duration
2626
GetSecretNamespace() string
2727
GetSecretName() string
28+
GetSecretLabels() map[string]string
29+
GetSecretAnnotations() map[string]string
2830
GetInstallationTokenOptions() *github.InstallationTokenOptions
2931
GetManagedSecret() githubv1.ManagedSecret
3032
UpdateManagedSecret() (changed bool)

internal/tokenmanager/token_secret.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,10 @@ func (s *tokenSecret) CreateSecret() error {
186186

187187
secret := &corev1.Secret{
188188
ObjectMeta: metav1.ObjectMeta{
189-
Namespace: s.owner.GetSecretNamespace(),
190-
Name: s.owner.GetSecretName(),
191-
Labels: s.SecretLabels(),
189+
Namespace: s.owner.GetSecretNamespace(),
190+
Name: s.owner.GetSecretName(),
191+
Labels: s.SecretLabels(),
192+
Annotations: s.owner.GetSecretAnnotations(),
192193
},
193194
Data: s.SecretData(installationToken.GetToken()),
194195
Type: secretType,
@@ -382,12 +383,16 @@ func (s *tokenSecret) UpdateTokenStatus(options ...tokenStatusOptions) error {
382383
}
383384

384385
func (s *tokenSecret) SecretLabels() map[string]string {
385-
return map[string]string{
386+
secretLabels := map[string]string{
386387
"app.kubernetes.io/name": s.owner.GetType(),
387388
"app.kubernetes.io/instance": s.owner.GetName(),
388389
"app.kubernetes.io/part-of": "github-token-manager",
389390
"app.kubernetes.io/created-by": "github-token-manager",
390391
}
392+
for k, v := range s.owner.GetSecretLabels() {
393+
secretLabels[k] = v
394+
}
395+
return secretLabels
391396
}
392397

393398
func (s *tokenSecret) SecretData(installationToken string) map[string][]byte {

0 commit comments

Comments
 (0)