Skip to content

Commit

Permalink
feat: extend githubcredential CR status (#195)
Browse files Browse the repository at this point in the history
fixes #188 
- shows to which repo, orgs or enterprises credentials are tied to
- adds unit tests for credentials controller
  • Loading branch information
rafalgalaw authored Oct 24, 2024
1 parent e4445d5 commit 851dc4d
Show file tree
Hide file tree
Showing 7 changed files with 1,023 additions and 221 deletions.
14 changes: 10 additions & 4 deletions api/v1beta1/githubcredentials_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ type GitHubCredentialSpec struct {

// GitHubCredentialStatus defines the observed state of GitHubCredential
type GitHubCredentialStatus struct {
ID int64 `json:"id"`
APIBaseURL string `json:"apiBaseUrl"`
UploadBaseURL string `json:"uploadBaseUrl"`
BaseURL string `json:"baseUrl"`
ID int64 `json:"id"`
APIBaseURL string `json:"apiBaseUrl"`
UploadBaseURL string `json:"uploadBaseUrl"`
BaseURL string `json:"baseUrl"`
Repositories []string `json:"repositories,omitempty"`
Organizations []string `json:"organizations,omitempty"`
Enterprises []string `json:"enterprises,omitempty"`

Conditions []metav1.Condition `json:"conditions,omitempty"`
}
Expand All @@ -43,6 +46,9 @@ type GitHubCredentialStatus struct {
//+kubebuilder:printcolumn:name="Error",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].message",priority=1
//+kubebuilder:printcolumn:name="AuthType",type="string",JSONPath=`.spec.authType`,description="Authentication type"
//+kubebuilder:printcolumn:name="GitHubEndpoint",type="string",JSONPath=`.spec.endpointRef.name`,description="GitHubEndpoint name these credentials are tied to"
//+kubebuilder:printcolumn:name="Repositories",type="string",JSONPath=`.status.repositories`,description="Repositories these credentials are tied to",priority=1
//+kubebuilder:printcolumn:name="Organizations",type="string",JSONPath=`.status.organizations`,description="Organizations these credentials are tied to",priority=1
//+kubebuilder:printcolumn:name="Enterprises",type="string",JSONPath=`.status.enterprises`,description="Enterprises these credentials are tied to",priority=1
//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time duration since creation of GitHubCredential"

// GitHubCredential is the Schema for the githubcredential API
Expand Down
15 changes: 15 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ spec:
jsonPath: .spec.endpointRef.name
name: GitHubEndpoint
type: string
- description: Repositories these credentials are tied to
jsonPath: .status.repositories
name: Repositories
priority: 1
type: string
- description: Organizations these credentials are tied to
jsonPath: .status.organizations
name: Organizations
priority: 1
type: string
- description: Enterprises these credentials are tied to
jsonPath: .status.enterprises
name: Enterprises
priority: 1
type: string
- description: Time duration since creation of GitHubCredential
jsonPath: .metadata.creationTimestamp
name: Age
Expand Down Expand Up @@ -196,9 +211,21 @@ spec:
- type
type: object
type: array
enterprises:
items:
type: string
type: array
id:
format: int64
type: integer
organizations:
items:
type: string
type: array
repositories:
items:
type: string
type: array
uploadBaseUrl:
type: string
required:
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ resources:
- bases/garm-operator.mercedes-benz.com_runners.yaml
- bases/garm-operator.mercedes-benz.com_garmserverconfigs.yaml
- bases/garm-operator.mercedes-benz.com_githubendpoints.yaml
- bases/garm-operator.mercedes-benz.com_githubcredentials.yaml
- bases/garm-operator.mercedes-benz.com_githubcredential.yaml
#+kubebuilder:scaffold:crdkustomizeresource

patches:
Expand Down
32 changes: 32 additions & 0 deletions internal/controller/githubcredentials_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,29 @@ func (r *GitHubCredentialReconciler) reconcileNormal(ctx context.Context, client
return ctrl.Result{}, err
}

// get detailed credentials, as update or list methods on garm client do not join repo, org and enterprise fields
res, err := client.GetCredentials(garmcredentials.NewGetCredentialsParams().WithID(int64(garmGitHubCreds.ID)))
if err != nil {
event.Error(r.Recorder, credentials, err.Error())
conditions.MarkFalse(credentials, conditions.ReadyCondition, conditions.GarmAPIErrorReason, err.Error())
if err := r.Status().Update(ctx, credentials); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, err
}
garmGitHubCreds = res.Payload

// set and update credentials status
credentials.Status.ID = int64(garmGitHubCreds.ID)
credentials.Status.BaseURL = garmGitHubCreds.BaseURL
credentials.Status.APIBaseURL = garmGitHubCreds.APIBaseURL
credentials.Status.UploadBaseURL = garmGitHubCreds.UploadBaseURL

repos, orgs, enterprises := getRepoOrgEnterpriseNames(garmGitHubCreds)
credentials.Status.Repositories = repos
credentials.Status.Organizations = orgs
credentials.Status.Enterprises = enterprises

conditions.MarkTrue(credentials, conditions.ReadyCondition, conditions.SuccessfulReconcileReason, "")
if err := r.Status().Update(ctx, credentials); err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -327,6 +345,20 @@ func (r *GitHubCredentialReconciler) findCredentialsForSecret(ctx context.Contex
return requests
}

func getRepoOrgEnterpriseNames(creds params.GithubCredentials) ([]string, []string, []string) {
var repos, orgs, enterprises []string
for _, repo := range creds.Repositories {
repos = append(repos, repo.Name)
}
for _, org := range creds.Organizations {
orgs = append(orgs, org.Name)
}
for _, ent := range creds.Enterprises {
enterprises = append(enterprises, ent.Name)
}
return repos, orgs, enterprises
}

// SetupWithManager sets up the controller with the Manager.
func (r *GitHubCredentialReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
Expand Down
Loading

0 comments on commit 851dc4d

Please sign in to comment.