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

     1  /*
     2  Copyright 2018 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  	corev1 "k8s.io/api/core/v1"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  	"k8s.io/apimachinery/pkg/runtime"
    23  
    24  	"knative.dev/pkg/apis"
    25  	"knative.dev/pkg/apis/duck/ducktypes"
    26  )
    27  
    28  // +genduck
    29  
    30  // PodSpecable is implemented by types containing a PodTemplateSpec
    31  // in the manner of ReplicaSet, Deployment, DaemonSet, StatefulSet.
    32  type PodSpecable corev1.PodTemplateSpec
    33  
    34  // PodSpecable is an Implementable duck type.
    35  var _ ducktypes.Implementable = (*PodSpecable)(nil)
    36  
    37  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
    38  
    39  // WithPod is the shell that demonstrates how PodSpecable types wrap
    40  // a PodSpec.
    41  type WithPod struct {
    42  	metav1.TypeMeta   `json:",inline"`
    43  	metav1.ObjectMeta `json:"metadata,omitempty"`
    44  
    45  	Spec WithPodSpec `json:"spec,omitempty"`
    46  }
    47  
    48  // WithPodSpec is the shell around the PodSpecable within WithPod.
    49  type WithPodSpec struct {
    50  	Template PodSpecable `json:"template,omitempty"`
    51  }
    52  
    53  // Verify WithPod resources meet duck contracts.
    54  var (
    55  	_ apis.Validatable      = (*WithPod)(nil)
    56  	_ apis.Defaultable      = (*WithPod)(nil)
    57  	_ apis.Listable         = (*WithPod)(nil)
    58  	_ ducktypes.Populatable = (*WithPod)(nil)
    59  )
    60  
    61  // GetFullType implements duck.Implementable
    62  func (wp *PodSpecable) GetFullType() ducktypes.Populatable {
    63  	return &WithPod{}
    64  }
    65  
    66  // Populate implements duck.Populatable
    67  func (wp *WithPod) Populate() {
    68  	wp.Spec.Template = PodSpecable{
    69  		ObjectMeta: metav1.ObjectMeta{
    70  			Labels: map[string]string{
    71  				"foo": "bar",
    72  			},
    73  		},
    74  		Spec: corev1.PodSpec{
    75  			Containers: []corev1.Container{{
    76  				Name:  "container-name",
    77  				Image: "container-image:latest",
    78  			}},
    79  		},
    80  	}
    81  }
    82  
    83  // GetListType implements apis.Listable
    84  func (wp *WithPod) GetListType() runtime.Object {
    85  	return &WithPodList{}
    86  }
    87  
    88  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
    89  
    90  // WithPodList is a list of WithPod resources
    91  type WithPodList struct {
    92  	metav1.TypeMeta `json:",inline"`
    93  	metav1.ListMeta `json:"metadata"`
    94  
    95  	Items []WithPod `json:"items"`
    96  }
    97  
    98  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
    99  
   100  // Pod is a wrapper around Pod-like resource, which supports our interfaces
   101  // for webhooks
   102  type Pod struct {
   103  	metav1.TypeMeta   `json:",inline"`
   104  	metav1.ObjectMeta `json:"metadata,omitempty"`
   105  
   106  	Spec corev1.PodSpec `json:"spec,omitempty"`
   107  }
   108  
   109  // Verify Pod resources meet duck contracts.
   110  var (
   111  	_ apis.Validatable      = (*Pod)(nil)
   112  	_ apis.Defaultable      = (*Pod)(nil)
   113  	_ apis.Listable         = (*Pod)(nil)
   114  	_ ducktypes.Populatable = (*Pod)(nil)
   115  )
   116  
   117  // GetFullType implements duck.Implementable
   118  func (p *Pod) GetFullType() ducktypes.Populatable {
   119  	return &Pod{}
   120  }
   121  
   122  // Populate implements duck.Populatable
   123  func (p *Pod) Populate() {
   124  	p.Spec = corev1.PodSpec{
   125  		Containers: []corev1.Container{{
   126  			Name:  "container-name",
   127  			Image: "container-image:latest",
   128  		}},
   129  	}
   130  }
   131  
   132  // GetListType implements apis.Listable
   133  func (p *Pod) GetListType() runtime.Object {
   134  	return &PodList{}
   135  }
   136  
   137  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
   138  
   139  // PodList is a list of WithPod resources
   140  type PodList struct {
   141  	metav1.TypeMeta `json:",inline"`
   142  	metav1.ListMeta `json:"metadata"`
   143  
   144  	Items []WithPod `json:"items"`
   145  }