Skip to content

Commit

Permalink
Merge pull request #6 from rmweir/managed-fields
Browse files Browse the repository at this point in the history
Syncing: Add logic and validation for managed fields
  • Loading branch information
rmweir committed Sep 13, 2020
2 parents 120eb0f + 5c8e3dd commit d553fcf
Show file tree
Hide file tree
Showing 8 changed files with 562 additions and 342 deletions.
544 changes: 311 additions & 233 deletions controller/eks-cluster-config-handler.go

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions crds/eksclusterconfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ spec:
imported:
type: boolean
kmsKey:
nullable: true
type: string
kubernetesVersion:
nullable: true
type: string
loggingTypes:
items:
Expand All @@ -45,6 +47,7 @@ spec:
nullable: true
type: string
gpu:
nullable: true
type: boolean
instanceType:
nullable: true
Expand All @@ -61,6 +64,7 @@ spec:
nullable: true
type: integer
nodegroupName:
nullable: true
type: string
subnets:
items:
Expand All @@ -82,8 +86,10 @@ spec:
nullable: true
type: array
privateAccess:
nullable: true
type: boolean
publicAccess:
nullable: true
type: boolean
publicAccessSources:
items:
Expand All @@ -93,13 +99,15 @@ spec:
region:
type: string
secretsEncryption:
nullable: true
type: boolean
securityGroups:
items:
type: string
nullable: true
type: array
serviceRole:
nullable: true
type: string
subnets:
items:
Expand All @@ -111,12 +119,6 @@ spec:
type: string
nullable: true
type: object
required:
- amazonCredentialSecret
- publicAccess
- privateAccess
- displayName
- region
type: object
status:
properties:
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ replace k8s.io/client-go => k8s.io/client-go v0.18.0
require (
github.com/aws/aws-sdk-go v1.30.22
github.com/blang/semver v3.5.0+incompatible
github.com/rancher/lasso v0.0.0-20200513231433-d0ce66327a25
github.com/rancher/wrangler v0.6.2-0.20200427172034-da9b142ae061
github.com/rancher/lasso v0.0.0-20200820172840-0e4cc0ef5cb0
github.com/rancher/wrangler v0.6.2-0.20200822010948-6d667521af49
github.com/rancher/wrangler-api v0.6.1-0.20200427172631-a7c2f09b783e
github.com/sirupsen/logrus v1.4.2
k8s.io/api v0.18.0
k8s.io/api v0.18.8
k8s.io/apiextensions-apiserver v0.18.0
k8s.io/apimachinery v0.18.0
k8s.io/apimachinery v0.18.8
k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible
)
177 changes: 175 additions & 2 deletions go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"context"
"flag"
"github.com/rancher/eks-operator/controller"
core2 "github.com/rancher/eks-operator/pkg/generated/controllers/core"
eksv1 "github.com/rancher/eks-operator/pkg/generated/controllers/eks.cattle.io"
core3 "github.com/rancher/wrangler/pkg/generated/controllers/core"

"github.com/rancher/wrangler-api/pkg/generated/controllers/apps"
"github.com/rancher/wrangler/pkg/kubeconfig"
Expand Down Expand Up @@ -41,7 +41,7 @@ func main() {
// Generated apps controller
apps := apps.NewFactoryFromConfigOrDie(cfg)
// core
core, err := core2.NewFactoryFromConfig(cfg)
core, err := core3.NewFactoryFromConfig(cfg)
if err != nil {
logrus.Fatalf("Error building core factory: %s", err.Error())
}
Expand Down
44 changes: 22 additions & 22 deletions pkg/apis/eks.cattle.io/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ type EKSClusterConfig struct {

// EKSClusterConfigSpec is the spec for a EKSClusterConfig resource
type EKSClusterConfigSpec struct {
KubernetesVersion string `json:"kubernetesVersion"`
AmazonCredentialSecret string `json:"amazonCredentialSecret"`
DisplayName string `json:"displayName" norman:"noupdate"`
Region string `json:"region" norman:"noupdate"`
Imported bool `json:"imported" norman:"noupdate"`
KubernetesVersion *string `json:"kubernetesVersion"`
Tags map[string]string `json:"tags"`
SecretsEncryption bool `json:"secretsEncryption" norman:"noupdate"`
KmsKey string `json:"kmsKey" norman:"noupdate"`
PublicAccess bool `json:"publicAccess" norman:"required"`
PrivateAccess bool `json:"privateAccess" norman:"required"`
SecretsEncryption *bool `json:"secretsEncryption" norman:"noupdate"`
KmsKey *string `json:"kmsKey" norman:"noupdate"`
PublicAccess *bool `json:"publicAccess"`
PrivateAccess *bool `json:"privateAccess"`
PublicAccessSources []string `json:"publicAccessSources"`
LoggingTypes []string `json:"loggingTypes"`
AmazonCredentialSecret string `json:"amazonCredentialSecret" norman:"required"`
DisplayName string `json:"displayName" norman:"required,noupdate"`
Subnets []string `json:"subnets" norman:"noupdate"`
SecurityGroups []string `json:"securityGroups" norman:"noupdate"`
ServiceRole string `json:"serviceRole" norman:"noupdate"`
Region string `json:"region" norman:"required,noupdate"`
Imported bool `json:"imported,omitempty" norman:"noupdate"`
ServiceRole *string `json:"serviceRole" norman:"noupdate"`
NodeGroups []NodeGroup `json:"nodeGroups"`
}

Expand All @@ -62,16 +62,16 @@ type EKSClusterConfigStatus struct {
}

type NodeGroup struct {
Gpu bool `json:"gpu"`
NodegroupName string `json:"nodegroupName" norman:"required"`
DiskSize *int64 `json:"diskSize"`
InstanceType *string `json:"instanceType" norman:"required"`
Labels map[string]*string `json:"labels"`
Ec2SshKey *string `json:"ec2SshKey"`
DesiredSize *int64 `json:"desiredSize"`
MaxSize *int64 `json:"maxSize"`
MinSize *int64 `json:"minSize"`
Subnets []string `json:"subnets"`
Tags map[string]*string `json:"tags"`
Version *string `json:"version"`
Gpu *bool `json:"gpu"`
NodegroupName *string `json:"nodegroupName" norman:"required"`
DiskSize *int64 `json:"diskSize"`
InstanceType *string `json:"instanceType" norman:"required"`
Labels map[string]*string `json:"labels"`
Ec2SshKey *string `json:"ec2SshKey"`
DesiredSize *int64 `json:"desiredSize"`
MaxSize *int64 `json:"maxSize"`
MinSize *int64 `json:"minSize"`
Subnets []string `json:"subnets"`
Tags map[string]*string `json:"tags"`
Version *string `json:"version"`
}
40 changes: 40 additions & 0 deletions pkg/apis/eks.cattle.io/v1/zz_generated_deepcopy.go

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

73 changes: 0 additions & 73 deletions pkg/codegen/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package main

import (
"encoding/json"
"fmt"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"os"

v12 "github.com/rancher/eks-operator/pkg/apis/eks.cattle.io/v1"
Expand Down Expand Up @@ -53,23 +51,6 @@ func main() {
panic(err)
}

// set additional validation
// removed anyOf for now, may be useful in future for api level validation
/*
// Openapi does not have a way of changing validation based of value directly. This approach instead
// requires the import field's value be true or the field be omitted. If the field is included it can
// be assumed that the cluster is imported, and therefore the required fields for an imported cluster
// will be validated. Otherwise, the required fields for a non-import cluster will be validated.
setOpenAPIEnum(obj.Spec.Validation.OpenAPIV3Schema.Properties, []interface{}{true}, "spec", "imported")
setOpenAPIAnyOf(obj.Spec.Validation.OpenAPIV3Schema.Properties, []v1beta1.JSONSchemaProps{
{
Required: []string{"imported", "amazonCredentialSecr", "displayName"},
},
{
Required: []string{"publicAccess", "privateAccess", "amazonCredentialSecr", "displayName"},
},
}, "spec")*/

eksCCYaml, err := yaml.Export(&obj)
if err != nil {
panic(err)
Expand All @@ -82,60 +63,6 @@ func main() {
fmt.Printf("obj yaml: %s", eksCCYaml)
}

func setOpenAPIRequired(properties map[string]v1beta1.JSONSchemaProps, required []string, keys ...string) {
if len(keys) == 0 {
return
}

if len(keys) == 1 {
propertyCopy := properties[keys[0]]
propertyCopy.Required = required
properties[keys[0]] = propertyCopy
}

setOpenAPIRequired(properties[keys[0]].Properties, required, keys[1:]...)
}

func setOpenAPIEnum(properties map[string]v1beta1.JSONSchemaProps, enumVals []interface{}, keys ...string) {
if len(keys) == 0 {
return
}

if len(keys) == 1 {
propertyCopy := properties[keys[0]]

var enum []v1beta1.JSON
for _, val := range enumVals {
j, err := json.Marshal(val)
if err != nil {
panic(err)
}

enum = append(
enum,
v1beta1.JSON{Raw: j})
}
propertyCopy.Enum = enum
properties[keys[0]] = propertyCopy
}

setOpenAPIEnum(properties[keys[0]].Properties, enumVals, keys[1:]...)
}

func setOpenAPIAnyOf(properties map[string]v1beta1.JSONSchemaProps, props []v1beta1.JSONSchemaProps, keys ...string) {
if len(keys) == 0 {
return
}

if len(keys) == 1 {
propertyCopy := properties[keys[0]]
propertyCopy.AnyOf = props
properties[keys[0]] = propertyCopy
}

setOpenAPIAnyOf(properties[keys[0]].Properties, props, keys[1:]...)
}

func newCRD(obj interface{}, customize func(crd.CRD) crd.CRD) crd.CRD {
crd := crd.CRD{
GVK: schema.GroupVersionKind{
Expand Down

0 comments on commit d553fcf

Please sign in to comment.