knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/duck/v1/auth_types.go (about)

     1  /*
     2  Copyright 2022 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v1
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  	"k8s.io/apimachinery/pkg/runtime/schema"
    26  	"knative.dev/pkg/apis"
    27  	"knative.dev/pkg/apis/duck/ducktypes"
    28  	"knative.dev/pkg/kmeta"
    29  	"knative.dev/pkg/ptr"
    30  )
    31  
    32  // +genduck
    33  
    34  // AuthStatus is meant to provide the generated service account name
    35  // in the resource status.
    36  type AuthStatus struct {
    37  	// ServiceAccountName is the name of the generated service account
    38  	// used for this components OIDC authentication.
    39  	ServiceAccountName *string `json:"serviceAccountName,omitempty"`
    40  
    41  	// ServiceAccountNames is the list of names of the generated service accounts
    42  	// used for this components OIDC authentication. This list can have len() > 1,
    43  	// when the component uses multiple identities (e.g. in case of a Parallel).
    44  	ServiceAccountNames []string `json:"serviceAccountNames,omitempty"`
    45  }
    46  
    47  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
    48  
    49  // AuthenticatableType is a skeleton type wrapping AuthStatus in the manner we expect
    50  // resource writers defining compatible resources to embed it.  We will
    51  // typically use this type to deserialize AuthenticatableType ObjectReferences and
    52  // access the AuthenticatableType data.  This is not a real resource.
    53  type AuthenticatableType struct {
    54  	metav1.TypeMeta   `json:",inline"`
    55  	metav1.ObjectMeta `json:"metadata,omitempty"`
    56  
    57  	Status AuthenticatableStatus `json:"status"`
    58  }
    59  
    60  type AuthenticatableStatus struct {
    61  	// Auth contains the service account name for the subscription
    62  	// +optional
    63  	Auth *AuthStatus `json:"auth,omitempty"`
    64  }
    65  
    66  var (
    67  	// AuthStatus is a Convertible type.
    68  	_ apis.Convertible = (*AuthStatus)(nil)
    69  
    70  	// Verify AuthenticatableType resources meet duck contracts.
    71  	_ apis.Listable         = (*AuthenticatableType)(nil)
    72  	_ ducktypes.Populatable = (*AuthenticatableType)(nil)
    73  	_ kmeta.OwnerRefable    = (*AuthenticatableType)(nil)
    74  )
    75  
    76  // GetFullType implements duck.Implementable
    77  func (*AuthStatus) GetFullType() ducktypes.Populatable {
    78  	return &AuthenticatableType{}
    79  }
    80  
    81  // ConvertTo implements apis.Convertible
    82  func (a *AuthStatus) ConvertTo(_ context.Context, to apis.Convertible) error {
    83  	return fmt.Errorf("v1 is the highest known version, got: %T", to)
    84  }
    85  
    86  // ConvertFrom implements apis.Convertible
    87  func (a *AuthStatus) ConvertFrom(_ context.Context, from apis.Convertible) error {
    88  	return fmt.Errorf("v1 is the highest known version, got: %T", from)
    89  }
    90  
    91  // Populate implements duck.Populatable
    92  func (t *AuthenticatableType) Populate() {
    93  	t.Status = AuthenticatableStatus{
    94  		Auth: &AuthStatus{
    95  			// Populate ALL fields
    96  			ServiceAccountName: ptr.String("foo"),
    97  			ServiceAccountNames: []string{
    98  				"bar",
    99  				"baz",
   100  			},
   101  		},
   102  	}
   103  }
   104  
   105  // GetGroupVersionKind implements kmeta.OwnerRefable
   106  func (t *AuthenticatableType) GetGroupVersionKind() schema.GroupVersionKind {
   107  	return t.GroupVersionKind()
   108  }
   109  
   110  // GetListType implements apis.Listable
   111  func (*AuthenticatableType) GetListType() runtime.Object {
   112  	return &AuthenticatableTypeList{}
   113  }
   114  
   115  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
   116  
   117  // AuthenticatableTypeList is a list of AuthenticatableType resources
   118  type AuthenticatableTypeList struct {
   119  	metav1.TypeMeta `json:",inline"`
   120  	metav1.ListMeta `json:"metadata"`
   121  
   122  	Items []AuthenticatableType `json:"items"`
   123  }