Skip to content

Commit

Permalink
feat: add resourcedistribution generator
Browse files Browse the repository at this point in the history
  • Loading branch information
dong4325 committed Aug 27, 2022
1 parent aedda20 commit 4955b4a
Show file tree
Hide file tree
Showing 7 changed files with 1,065 additions and 0 deletions.
91 changes: 91 additions & 0 deletions cmd/resourcedistributiongenerator/generator/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Copyright 2022 The Kruise Authors.
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 generator

const tmpl = `
apiVersion: apps.kruise.io/v1alpha1
kind: ResourceDistribution
spec:
resource:
apiVersion: v1
`

// Field names
const (
kindField = "kind"
nameField = "name"
listField = "list"
allNamespacesField = "allNamespaces"
immutableField = "immutable"
typeField = "type"
matchExpressionsField = "matchExpressions"
keyField = "key"
operatorField = "operator"
valuesField = "values"
)

var metadataLabelsPath = []string{"metadata", "labels"}
var metadataAnnotationsPath = []string{"metadata", "annotations"}
var resourcePath = []string{"spec", "resource"}
var metadataPath = []string{"spec", "resource", "metadata"}
var resourceLabelsPath = []string{"spec", "resource", "metadata", "labels"}
var resourceAnnotationsPath = []string{"spec", "resource", "metadata", "annotations"}
var targetsPath = []string{"spec", "targets"}
var includedNamespacesPath = []string{"spec", "targets", "includedNamespaces"}
var excludedNamespacesPath = []string{"spec", "targets", "excludedNamespaces"}
var NamespaceLabelSelectorPath = []string{"spec", "targets", "namespaceLabelSelector"}
var MatchLabelsPath = []string{"spec", "targets", "namespaceLabelSelector", "matchLabels"}

const TestData = `
apiVersion: config.kubernetes.io/v1
kind: ResourceList
items:
functionConfig:
apiVersion: apps.kruise.io/v1alpha1
kind: ResourceDistributionGenerator
metadata:
name: rdname
resource:
resourceKind: ConfigMap
resourceName: cmname
literals:
- JAVA_HOME=/opt/java/jdk
resourceOptions:
annotations:
dashboard: "1"
immutable: true
labels:
rsla: rs
options:
labels:
app.kubernetes.io/name: "app1"
annotations:
an: rdan
targets:
allNamespaces: true
includedNamespaces:
- ns-1
namespaceLabelSelector:
matchLabels:
group: "test"
matchExpressions:
- key: exc
operator: NotIn
values:
- abc
- e
`
108 changes: 108 additions & 0 deletions cmd/resourcedistributiongenerator/generator/resourcedistribution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Copyright 2022 The Kruise Authors.
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 generator

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

type ResourceDistributionPlugin struct {
types.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"`
ResourceArgs `json:"resource,omitempty" yaml:"resource,omitempty"`
TargetsArgs `json:"targets,omitempty" yaml:"targets,omitempty"`

// Options for the resourcedistribution.
// GeneratorOptions same as configmap and secret generator Options
Options *types.GeneratorOptions `json:"options,omitempty" yaml:"options,omitempty"`

// Behavior of generated resource, must be one of:
// 'create': create a new one
// 'replace': replace the existing one
// 'merge': merge with the existing one
Behavior string `json:"behavior,omitempty" yaml:"behavior,omitempty"`
}

// ResourceArgs contain arguments for the resource to be distributed.
type ResourceArgs struct {
// Name of the resource to be distributed.
ResourceName string `json:"resourceName,omitempty" yaml:"resourceName,omitempty"`
// Only configmap and secret are available
ResourceKind string `json:"resourceKind,omitempty" yaml:"resourceKind,omitempty"`

// KvPairSources defines places to obtain key value pairs.
// same as configmap and secret generator KvPairSources
types.KvPairSources `json:",inline,omitempty" yaml:",inline,omitempty"`

// Options for the resource to be distributed.
// GeneratorOptions same as configmap and secret generator Options
ResourceOptions *types.GeneratorOptions `json:"resourceOptions,omitempty" yaml:"resourceOptions,omitempty"`

// Type of the secret. It can be "Opaque" (default), or "kubernetes.io/tls".
//
// If type is "kubernetes.io/tls", then "literals" or "files" must have exactly two
// keys: "tls.key" and "tls.crt"
Type string `json:"type,omitempty" yaml:"type,omitempty"`
}

// TargetsArgs defines places to obtain target namespace args.
type TargetsArgs struct {
// AllNamespaces if true distribute all namespaces
AllNamespaces bool `json:"allNamespaces,omitempty" yaml:"allNamespaces,omitempty"`

// ExcludedNamespaces is a list of excluded namespaces name.
ExcludedNamespaces []string `json:"excludedNamespaces,omitempty" yaml:"excludedNamespaces,omitempty"`

// IncludedNamespaces is a list of included namespaces name.
IncludedNamespaces []string `json:"includedNamespaces,omitempty" yaml:"includedNamespaces,omitempty"`

// NamespaceLabelSelector for the generator.
NamespaceLabelSelector *metav1.LabelSelector `json:"namespaceLabelSelector,omitempty" yaml:"namespaceLabelSelector,omitempty"`
}

// MakeResourceDistribution makes a ResourceDistribution.
//
// ResourceDistribution: https://openkruise.io/docs/user-manuals/resourcedistribution
func MakeResourceDistribution(config *ResourceDistributionPlugin) (*yaml.RNode, error) {
rn, err := makeBaseNode(&config.ObjectMeta)
if err != nil {
return nil, err
}

// set Labels and Annotations for ResourceDistribution
err = setLabelsOrAnnotations(rn, config.Options.Labels, metadataLabelsPath)
if err != nil {
return nil, err
}
err = setLabelsOrAnnotations(rn, config.Options.Annotations, metadataAnnotationsPath)
if err != nil {
return nil, err
}

err = setResource(rn, &config.ResourceArgs)
if err != nil {
return nil, err
}

err = setTargets(rn, &config.TargetsArgs)
if err != nil {
return nil, err
}

return rn, nil
}
Loading

0 comments on commit 4955b4a

Please sign in to comment.