Skip to content

Commit

Permalink
Define HNCConfiguration CR and set up a reconciler for the CR.
Browse files Browse the repository at this point in the history
This commit implements HNC type configuration by creating HNCConfiguration CR and setting up a corresponding reconciler for it. Specifically, it does the following:

 - Implements the Go structs for HNCConfiguration and generates corresponding yaml files by running "make manifests".
 - Creates a basic reconciler for HNCConfiguration and configures it in setup.go. The reconciler does the following:
    - Creates a default singleton if it does not exist.
    - Writes or updates the singleton on the apiserver.
    - Logs the Spec of the HNCConfiguration when a reconciliation happens.

Tested:
  - Verified that the CRD were created correctly.
  - Created a sample HNCConfiguration and applied to KIND.
  - Verified the logs are expected on a GKE cluster during a reconciliation.

Design doc: http://bit.ly/hnc-type-configuration
Issue: kubernetes-retired#411
  • Loading branch information
sophieliu15 committed Feb 12, 2020
1 parent fe9fd06 commit 7ab6c83
Show file tree
Hide file tree
Showing 9 changed files with 487 additions and 1 deletion.
104 changes: 104 additions & 0 deletions incubator/hnc/api/v1alpha1/hnc_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// Constants for types and well-known names.
const (
HNCConfigSingleton = "config"
)

// SynchronizationMode describes propogation mode of objects of the same kind.
// The only three modes currently supported are "propagate", "ignore", and "remove".
// See detailed definition below. An unsupported mode will be treated as "ignore".
type SynchronizationMode string

const (
// Propagate objects from ancestors to descendants and deletes obsolete descendants.
Propagate SynchronizationMode = "propagate"

// Ignore the modification of this type. New or changed objects will not be propagated,
// and obsolete objects will not be deleted. The inheritedFrom label is not removed.
// Any unknown mode is treated as ignore.
Ignore SynchronizationMode = "ignore"

// Remove all existing propagated copies.
Remove SynchronizationMode = "remove"
)

// TypeSynchronizationSpec defines the desired synchronization state of a specific kind.
type TypeSynchronizationSpec struct {
// API version of the kind defined below. This is used to unambiguously identifies the kind.
APIVersion string `json:"apiVersion,omitempty"`
// Kind to be configured.
Kind string `json:"kind,omitempty"`
// Synchronization mode of the kind.
// +optional
Mode SynchronizationMode `json:"mode,omitempty"`
}

// TypeSynchronizationStatus defines the observed synchronization state of a specific kind.
type TypeSynchronizationStatus struct {
// API version of the kind defined below. This is used to unambiguously identifies the kind.
APIVersion string `json:"apiVersion,omitempty"`
// Kind to be configured.
Kind string `json:"kind,omitempty"`

// Tracks the number of original objects that are being propagated to descendant namespaces.
// +kubebuilder:validation:Minimum=0
// +optional
NumPropagated *int32 `json:"numPropagated,omitempty"`
}

// +kubebuilder:object:root=true
// +kubebuilder:resource:path=hncconfigurations,scope=Cluster

// HNCConfiguration is a cluster-wide configuration for HNC as a whole. See details in http://bit.ly/hnc-type-configuration
type HNCConfiguration struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec HNCConfigurationSpec `json:"spec,omitempty"`
Status HNCConfigurationStatus `json:"status,omitempty"`
}

// HNCConfigurationSpec defines the desired state of HNC configuration.
type HNCConfigurationSpec struct {
// Types indicates the desired synchronization states of kinds, if any.
Types []TypeSynchronizationSpec `json:"types,omitempty"`
}

// HNCConfigurationStatus defines the observed state of HNC configuration.
type HNCConfigurationStatus struct {
// Types indicates the observed synchronization states of kinds, if any.
Types []TypeSynchronizationStatus `json:"types,omitempty"`
}

// +kubebuilder:object:root=true

// HNCConfigurationList contains a list of HNCConfiguration.
type HNCConfigurationList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []HNCConfiguration `json:"items"`
}

func init() {
SchemeBuilder.Register(&HNCConfiguration{}, &HNCConfigurationList{})
}
136 changes: 136 additions & 0 deletions incubator/hnc/api/v1alpha1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.2.4
creationTimestamp: null
name: hierarchyconfigurations.hnc.x-k8s.io
spec:
Expand All @@ -12,7 +14,7 @@ spec:
listKind: HierarchyConfigurationList
plural: hierarchyconfigurations
singular: hierarchyconfiguration
scope: ""
scope: Namespaced
validation:
openAPIV3Schema:
description: Hierarchy is the Schema for the hierarchies API
Expand Down
95 changes: 95 additions & 0 deletions incubator/hnc/config/crd/bases/hnc.x-k8s.io_hncconfigurations.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@

---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.2.4
creationTimestamp: null
name: hncconfigurations.hnc.x-k8s.io
spec:
group: hnc.x-k8s.io
names:
kind: HNCConfiguration
listKind: HNCConfigurationList
plural: hncconfigurations
singular: hncconfiguration
scope: Cluster
validation:
openAPIV3Schema:
description: HNCConfiguration is a cluster-wide configuration for HNC as a whole.
See details in http://bit.ly/hnc-type-configuration
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: HNCConfigurationSpec defines the desired state of HNC configuration.
properties:
types:
description: Types indicates the desired synchronization states of kinds,
if any.
items:
description: TypeSynchronizationSpec defines the desired synchronization
state of a specific kind.
properties:
apiVersion:
description: API version of the kind defined below. This is used
to unambiguously identifies the kind.
type: string
kind:
description: Kind to be configured.
type: string
mode:
description: Synchronization mode of the kind.
type: string
type: object
type: array
type: object
status:
description: HNCConfigurationStatus defines the observed state of HNC configuration.
properties:
types:
description: Types indicates the observed synchronization states of
kinds, if any.
items:
description: TypeSynchronizationStatus defines the observed synchronization
state of a specific kind.
properties:
apiVersion:
description: API version of the kind defined below. This is used
to unambiguously identifies the kind.
type: string
kind:
description: Kind to be configured.
type: string
numPropagated:
description: Tracks the number of original objects that are being
propagated to descendant namespaces.
format: int32
minimum: 0
type: integer
type: object
type: array
type: object
type: object
version: v1alpha1
versions:
- name: v1alpha1
served: true
storage: true
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []
1 change: 1 addition & 0 deletions incubator/hnc/config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# It should be run by config/default
resources:
- bases/hnc.x-k8s.io_hierarchyconfigurations.yaml
- bases/hnc.x-k8s.io_hncconfigurations.yaml
# +kubebuilder:scaffold:crdkustomizeresource

patchesStrategicMerge:
Expand Down
12 changes: 12 additions & 0 deletions incubator/hnc/config/samples/hnc_v1_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: hnc.x-k8s.io/v1alpha1
kind: HNCConfiguration
metadata:
name: config
spec:
types:
- apiVersion: v1
kind: Secret
mode: propagate
- apiVersion: rbac.authorization.k8s.io/v1
kind: Role
mode: ignore
Loading

0 comments on commit 7ab6c83

Please sign in to comment.