Skip to content

Commit

Permalink
Merge pull request #53 from Benedikt1992/implement_deploy_token_for_g…
Browse files Browse the repository at this point in the history
…roups

Implement deploy token for groups
  • Loading branch information
janwillies authored Jan 18, 2022
2 parents bd4b739 + c726275 commit b029a8c
Show file tree
Hide file tree
Showing 19 changed files with 1,501 additions and 57 deletions.
101 changes: 101 additions & 0 deletions apis/groups/v1alpha1/deploytoken_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
Copyright 2021 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
)

// DeployTokenParameters define the desired state of a Gitlab deploy token
// https://docs.gitlab.com/ee/api/deploy_tokens.html
type DeployTokenParameters struct {
// GroupID is the ID of the group to create the deploy token in.
// +optional
// +immutable
GroupID *int `json:"groupId,omitempty"`

// GroupIDRef is a reference to a group to retrieve its groupId
// +optional
// +immutable
GroupIDRef *xpv1.Reference `json:"groupIdRef,omitempty"`

// GroupIDSelector selects reference to a group to retrieve its groupId.
// +optional
GroupIDSelector *xpv1.Selector `json:"groupIdSelector,omitempty"`

// Expiration date for the deploy token. Does not expire if no value is provided.
// Expected in ISO 8601 format (2019-03-15T08:00:00Z)
// +optional
// +immutable
ExpiresAt *metav1.Time `json:"expiresAt,omitempty"`

// Username for deploy token. Default is gitlab+deploy-token-{n}
// +optional
// +immutable
Username *string `json:"username,omitempty"`

// Scopes indicates the deploy token scopes.
// Must be at least one of read_repository, read_registry, write_registry,
// read_package_registry, or write_package_registry.
// +immutable
Scopes []string `json:"scopes"`
}

// DeployTokenObservation represents a deploy token.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/deploy_tokens.html
type DeployTokenObservation struct{}

// A DeployTokenSpec defines the desired state of a Gitlab Group.
type DeployTokenSpec struct {
xpv1.ResourceSpec `json:",inline"`
ForProvider DeployTokenParameters `json:"forProvider"`
}

// A DeployTokenStatus represents the observed state of a Gitlab Group.
type DeployTokenStatus struct {
xpv1.ResourceStatus `json:",inline"`
AtProvider DeployTokenObservation `json:"atProvider,omitempty"`
}

// +kubebuilder:object:root=true

// A DeployToken is a managed resource that represents a Gitlab deploy token
// +kubebuilder:printcolumn:name="READY",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].status"
// +kubebuilder:printcolumn:name="SYNCED",type="string",JSONPath=".status.conditions[?(@.type=='Synced')].status"
// +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp"
// +kubebuilder:subresource:status
// +kubebuilder:resource:scope=Cluster,categories={crossplane,managed,gitlab}
type DeployToken struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec DeployTokenSpec `json:"spec"`
Status DeployTokenStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// DeployTokenList contains a list of Group items
type DeployTokenList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []DeployToken `json:"items"`
}
23 changes: 23 additions & 0 deletions apis/groups/v1alpha1/referencers.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,26 @@ func (mg *Member) ResolveReferences(ctx context.Context, c client.Reader) error

return nil
}

// ResolveReferences of this Deploy Token
func (mg *DeployToken) ResolveReferences(ctx context.Context, c client.Reader) error {
r := reference.NewAPIResolver(c, mg)

// resolve spec.forProvider.projectIdRef
rsp, err := r.Resolve(ctx, reference.ResolutionRequest{
CurrentValue: fromPtrValue(mg.Spec.ForProvider.GroupID),
Reference: mg.Spec.ForProvider.GroupIDRef,
Selector: mg.Spec.ForProvider.GroupIDSelector,
To: reference.To{Managed: &Group{}, List: &GroupList{}},
Extract: reference.ExternalName(),
})

if err != nil {
return errors.Wrap(err, "spec.forProvider.groupId")
}

mg.Spec.ForProvider.GroupID = toPtrValue(rsp.ResolvedValue)
mg.Spec.ForProvider.GroupIDRef = rsp.ResolvedReference

return nil
}
9 changes: 9 additions & 0 deletions apis/groups/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,16 @@ var (
MemberKubernetesGroupVersionKind = SchemeGroupVersion.WithKind(MemberKind)
)

// Deploy Token type metadata
var (
DeployTokenKind = reflect.TypeOf(DeployToken{}).Name()
DeployTokenGroupKind = schema.GroupKind{Group: KubernetesGroup, Kind: DeployTokenKind}.String()
DeployTokenKindAPIVersion = DeployTokenKind + "." + SchemeGroupVersion.String()
DeployTokenGroupVersionKind = SchemeGroupVersion.WithKind(DeployTokenKind)
)

func init() {
SchemeBuilder.Register(&Group{}, &GroupList{})
SchemeBuilder.Register(&Member{}, &MemberList{})
SchemeBuilder.Register(&DeployToken{}, &DeployTokenList{})
}
152 changes: 152 additions & 0 deletions apis/groups/v1alpha1/zz_generated.deepcopy.go

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

56 changes: 56 additions & 0 deletions apis/groups/v1alpha1/zz_generated.managed.go

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

9 changes: 9 additions & 0 deletions apis/groups/v1alpha1/zz_generated.managedlist.go

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

Loading

0 comments on commit b029a8c

Please sign in to comment.