github.com/rancher/types@v0.0.0-20220328215343-4370ff10ecd5/apis/cluster.cattle.io/v3/schema/schema.go (about)

     1  package schema
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  
     7  	"github.com/rancher/norman/types"
     8  	m "github.com/rancher/norman/types/mapper"
     9  	v3 "github.com/rancher/types/apis/cluster.cattle.io/v3"
    10  	"github.com/rancher/types/factory"
    11  	v1 "k8s.io/api/core/v1"
    12  	storagev1 "k8s.io/api/storage/v1"
    13  	apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
    14  )
    15  
    16  var (
    17  	Version = types.APIVersion{
    18  		Version:          "v3",
    19  		Group:            "cluster.cattle.io",
    20  		Path:             "/v3/cluster",
    21  		SubContext:       true,
    22  		SubContextSchema: "/v3/schemas/cluster",
    23  	}
    24  
    25  	Schemas = factory.Schemas(&Version).
    26  		Init(namespaceTypes).
    27  		Init(persistentVolumeTypes).
    28  		Init(storageClassTypes).
    29  		Init(tokens).
    30  		Init(apiServiceTypes)
    31  )
    32  
    33  func namespaceTypes(schemas *types.Schemas) *types.Schemas {
    34  	return schemas.
    35  		AddMapperForType(&Version, v1.NamespaceSpec{},
    36  			&m.Drop{Field: "finalizers"},
    37  		).
    38  		AddMapperForType(&Version, v1.Namespace{},
    39  			&m.AnnotationField{Field: "description"},
    40  			&m.AnnotationField{Field: "projectId"},
    41  			&m.AnnotationField{Field: "resourceQuota", Object: true},
    42  			&m.AnnotationField{Field: "containerDefaultResourceLimit", Object: true},
    43  			&m.Drop{Field: "status"},
    44  		).
    45  		MustImport(&Version, NamespaceResourceQuota{}).
    46  		MustImport(&Version, ContainerResourceLimit{}).
    47  		MustImport(&Version, v1.Namespace{}, struct {
    48  			Description                   string `json:"description"`
    49  			ProjectID                     string `norman:"type=reference[/v3/schemas/project],noupdate"`
    50  			ResourceQuota                 string `json:"resourceQuota,omitempty" norman:"type=namespaceResourceQuota"`
    51  			ContainerDefaultResourceLimit string `json:"containerDefaultResourceLimit,omitempty" norman:"type=containerResourceLimit"`
    52  		}{}).
    53  		MustImport(&Version, NamespaceMove{}).
    54  		MustImportAndCustomize(&Version, v1.Namespace{}, func(schema *types.Schema) {
    55  			schema.ResourceActions["move"] = types.Action{
    56  				Input: "namespaceMove",
    57  			}
    58  		})
    59  }
    60  
    61  func persistentVolumeTypes(schemas *types.Schemas) *types.Schemas {
    62  	return schemas.
    63  		AddMapperForType(&Version, v1.PersistentVolume{},
    64  			&m.AnnotationField{Field: "description"},
    65  		).
    66  		AddMapperForType(&Version, v1.HostPathVolumeSource{},
    67  			m.Move{From: "type", To: "kind"},
    68  			m.Enum{
    69  				Options: []string{
    70  					"DirectoryOrCreate",
    71  					"Directory",
    72  					"FileOrCreate",
    73  					"File",
    74  					"Socket",
    75  					"CharDevice",
    76  					"BlockDevice",
    77  				},
    78  				Field: "kind",
    79  			},
    80  		).
    81  		MustImport(&Version, v1.PersistentVolumeSpec{}, struct {
    82  			StorageClassName *string `json:"storageClassName,omitempty" norman:"type=reference[storageClass]"`
    83  		}{}).
    84  		MustImport(&Version, v1.PersistentVolume{}, struct {
    85  			Description string `json:"description"`
    86  		}{}).
    87  		MustImportAndCustomize(&Version, v1.PersistentVolume{}, func(schema *types.Schema) {
    88  			schema.MustCustomizeField("name", func(field types.Field) types.Field {
    89  				field.Type = "hostname"
    90  				field.Nullable = false
    91  				field.Required = true
    92  				return field
    93  			})
    94  			schema.MustCustomizeField("volumeMode", func(field types.Field) types.Field {
    95  				field.Update = false
    96  				return field
    97  			})
    98  			// All fields of PersistentVolumeSource are immutable
    99  			val := reflect.ValueOf(v1.PersistentVolumeSource{})
   100  			for i := 0; i < val.Type().NumField(); i++ {
   101  				if tag, ok := val.Type().Field(i).Tag.Lookup("json"); ok {
   102  					name := strings.Split(tag, ",")[0]
   103  					schema.MustCustomizeField(name, func(field types.Field) types.Field {
   104  						field.Update = false
   105  						return field
   106  					})
   107  					pvSchema := schemas.Schema(&Version, val.Type().Field(i).Type.String()[4:])
   108  					for name := range pvSchema.ResourceFields {
   109  						pvSchema.MustCustomizeField(name, func(field types.Field) types.Field {
   110  							field.Update = false
   111  							return field
   112  						})
   113  					}
   114  				}
   115  			}
   116  		})
   117  }
   118  
   119  func storageClassTypes(schemas *types.Schemas) *types.Schemas {
   120  	return schemas.
   121  		AddMapperForType(&Version, storagev1.StorageClass{},
   122  			&m.AnnotationField{Field: "description"},
   123  		).
   124  		MustImport(&Version, storagev1.StorageClass{}, struct {
   125  			Description   string `json:"description"`
   126  			ReclaimPolicy string `json:"reclaimPolicy,omitempty" norman:"type=enum,options=Recycle|Delete|Retain"`
   127  		}{})
   128  }
   129  
   130  func tokens(schemas *types.Schemas) *types.Schemas {
   131  	return schemas.
   132  		MustImportAndCustomize(&Version, v3.ClusterAuthToken{}, func(schema *types.Schema) {
   133  			schema.CollectionMethods = []string{}
   134  			schema.ResourceMethods = []string{}
   135  		}).
   136  		MustImportAndCustomize(&Version, v3.ClusterUserAttribute{}, func(schema *types.Schema) {
   137  			schema.CollectionMethods = []string{}
   138  			schema.ResourceMethods = []string{}
   139  		})
   140  }
   141  
   142  func apiServiceTypes(Schemas *types.Schemas) *types.Schemas {
   143  	return Schemas.
   144  		AddMapperForType(&Version, apiregistrationv1.APIService{},
   145  			&m.Embed{Field: "status"},
   146  		).
   147  		MustImport(&Version, apiregistrationv1.APIService{})
   148  }