Skip to content

Commit

Permalink
Add AuthenticatableType duck type (#3056)
Browse files Browse the repository at this point in the history
* Add AuthenticatableType

* Add Resolver for AuthenticatableType

* Run gofmt and goimports

* Fix linter issues
  • Loading branch information
creydr authored Jun 14, 2024
1 parent 15e6cdf commit 339c22b
Show file tree
Hide file tree
Showing 6 changed files with 655 additions and 0 deletions.
93 changes: 93 additions & 0 deletions apis/duck/v1/auth_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ limitations under the License.

package v1

import (
"context"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/pkg/apis"
"knative.dev/pkg/apis/duck/ducktypes"
"knative.dev/pkg/kmeta"
"knative.dev/pkg/ptr"
)

// +genduck

// AuthStatus is meant to provide the generated service account name
// in the resource status.
type AuthStatus struct {
Expand All @@ -28,3 +43,81 @@ type AuthStatus struct {
// when the component uses multiple identities (e.g. in case of a Parallel).
ServiceAccountNames []string `json:"serviceAccountNames,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// AuthenticatableType is a skeleton type wrapping AuthStatus in the manner we expect
// resource writers defining compatible resources to embed it. We will
// typically use this type to deserialize AuthenticatableType ObjectReferences and
// access the AuthenticatableType data. This is not a real resource.
type AuthenticatableType struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Status AuthenticatableStatus `json:"status"`
}

type AuthenticatableStatus struct {
// Auth contains the service account name for the subscription
// +optional
Auth *AuthStatus `json:"auth,omitempty"`
}

var (
// AuthStatus is a Convertible type.
_ apis.Convertible = (*AuthStatus)(nil)

// Verify AuthenticatableType resources meet duck contracts.
_ apis.Listable = (*AuthenticatableType)(nil)
_ ducktypes.Populatable = (*AuthenticatableType)(nil)
_ kmeta.OwnerRefable = (*AuthenticatableType)(nil)
)

// GetFullType implements duck.Implementable
func (*AuthStatus) GetFullType() ducktypes.Populatable {
return &AuthenticatableType{}
}

// ConvertTo implements apis.Convertible
func (a *AuthStatus) ConvertTo(_ context.Context, to apis.Convertible) error {
return fmt.Errorf("v1 is the highest known version, got: %T", to)
}

// ConvertFrom implements apis.Convertible
func (a *AuthStatus) ConvertFrom(_ context.Context, from apis.Convertible) error {
return fmt.Errorf("v1 is the highest known version, got: %T", from)
}

// Populate implements duck.Populatable
func (t *AuthenticatableType) Populate() {
t.Status = AuthenticatableStatus{
Auth: &AuthStatus{
// Populate ALL fields
ServiceAccountName: ptr.String("foo"),
ServiceAccountNames: []string{
"bar",
"baz",
},
},
}
}

// GetGroupVersionKind implements kmeta.OwnerRefable
func (t *AuthenticatableType) GetGroupVersionKind() schema.GroupVersionKind {
return t.GroupVersionKind()
}

// GetListType implements apis.Listable
func (*AuthenticatableType) GetListType() runtime.Object {
return &AuthenticatableTypeList{}
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// AuthenticatableTypeList is a list of AuthenticatableType resources
type AuthenticatableTypeList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`

Items []AuthenticatableType `json:"items"`
}
81 changes: 81 additions & 0 deletions apis/duck/v1/zz_generated.deepcopy.go

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

60 changes: 60 additions & 0 deletions client/injection/ducks/duck/v1/authstatus/authstatus.go

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

30 changes: 30 additions & 0 deletions client/injection/ducks/duck/v1/authstatus/fake/fake.go

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

Loading

0 comments on commit 339c22b

Please sign in to comment.