Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Up access token secret name to be configurable in GithubApp spec #65

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ It will reconcile a new access token before expiry (1hr).
Key features:
- Uses a custom resource `GithubApp` in your destination namespace.
- Reads `appId`, `installId` and either and `privateKeySecret` or `vaultPrivateKey` defined in a `GithubApp` resource and requests an access token from Github for the Github App.
- It stores the access token in a secret `github-app-access-token-{appId}`
- It stores the access token in a secret as per `accessTokenSecret`
- For pulling a GitHub Apps private key, there are 2 options built-in:
- Using a Kubernetes secret:
- Use `privateKeySecret` - refers to an existing secret in the namespace which holds the base64 encoded PEM of the Github App's private key.
Expand Down Expand Up @@ -60,6 +60,7 @@ Key features:
- INSTALL ID
- EXPIRES AT
- ERROR
- Access Token Secret
- Events are recorded for:
- Any error on reconcile for a GithubApp
- Creation of an access token secret
Expand Down Expand Up @@ -96,6 +97,7 @@ spec:
appId: 123123
installId: 12312312
privateKeySecret: github-app-secret
accessTokenSecret: github-app-access-token-123123
EOF
```

Expand All @@ -114,6 +116,7 @@ spec:
appId: 123123
installId: 12312312
privateKeySecret: github-app-secret
accessTokenSecret: github-app-access-token-123123
rolloutDeployment:
labels:
foo: bar
Expand All @@ -133,6 +136,7 @@ metadata:
spec:
appId: 123123
installId: 12312312
accessTokenSecret: github-app-access-token-123123
vaultPrivateKey:
mountPath: secret
secretPath: githubapp/123123
Expand Down
2 changes: 2 additions & 0 deletions api/v1/githubapp_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type GithubAppSpec struct {
PrivateKeySecret string `json:"privateKeySecret,omitempty"`
RolloutDeployment *RolloutDeploymentSpec `json:"rolloutDeployment,omitempty"`
VaultPrivateKey *VaultPrivateKeySpec `json:"vaultPrivateKey,omitempty"`
AccessTokenSecret string `json:"accessTokenSecret"`
}

// GithubAppStatus defines the observed state of GithubApp
Expand All @@ -42,6 +43,7 @@ type GithubAppStatus struct {

// GithubApp is the Schema for the githubapps API
// +kubebuilder:printcolumn:name="App ID",type=string,JSONPath=`.spec.appId`
// +kubebuilder:printcolumn:name="Access Token Secret",type=string,JSONPath=`.spec.accessTokenSecret`
// +kubebuilder:printcolumn:name="Install ID",type=string,JSONPath=`.spec.installId`
// +kubebuilder:printcolumn:name="Expires At",type=string,JSONPath=`.status.expiresAt`
// +kubebuilder:printcolumn:name="Error",type=string,JSONPath=`.status.error`
Expand Down
6 changes: 6 additions & 0 deletions config/crd/bases/githubapp.samir.io_githubapps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ spec:
- jsonPath: .spec.appId
name: App ID
type: string
- jsonPath: .spec.accessTokenSecret
name: Access Token Secret
type: string
- jsonPath: .spec.installId
name: Install ID
type: string
Expand Down Expand Up @@ -52,6 +55,8 @@ spec:
spec:
description: GithubAppSpec defines the desired state of GithubApp
properties:
accessTokenSecret:
type: string
appId:
type: integer
installId:
Expand Down Expand Up @@ -83,6 +88,7 @@ spec:
- secretPath
type: object
required:
- accessTokenSecret
- appId
- installId
type: object
Expand Down
1 change: 1 addition & 0 deletions example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ spec:
appId: 857468
installId: 48531286
privateKeySecret: github-app-secret
accessTokenSecret: github-app-access-token-123123
4 changes: 2 additions & 2 deletions internal/controller/githubapp_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (r *GithubAppReconciler) checkExpiryAndUpdateAccessToken(ctx context.Contex
// Check if the access token secret exists if not reconcile immediately
accessTokenSecretKey := client.ObjectKey{
Namespace: githubApp.Namespace,
Name: fmt.Sprintf("github-app-access-token-%d", githubApp.Spec.AppId),
Name: githubApp.Spec.AccessTokenSecret,
}
accessTokenSecret := &corev1.Secret{}
if err := r.Get(ctx, accessTokenSecretKey, accessTokenSecret); err != nil {
Expand Down Expand Up @@ -659,7 +659,7 @@ func (r *GithubAppReconciler) createOrUpdateAccessToken(ctx context.Context, git
}

// Access token Kubernetes secret name
accessTokenSecret := fmt.Sprintf("github-app-access-token-%d", githubApp.Spec.AppId)
accessTokenSecret := githubApp.Spec.AccessTokenSecret

// Access token secret key
accessTokenSecretKey := client.ObjectKey{
Expand Down
1 change: 1 addition & 0 deletions internal/controller/test_helpers/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func CreateGitHubAppAndWait(
PrivateKeySecret: privateKeySecret,
RolloutDeployment: rolloutDeploymentSpec, // Optionally pass rolloutDeployment
VaultPrivateKey: vaultPrivateKeySpec, // Optionally pass vaultPrivateKeySpec
AccessTokenSecret: acessTokenSecretName,
},
}
gomega.Expect(k8sClient.Create(ctx, &githubApp)).Should(gomega.Succeed())
Expand Down
Loading