-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add resourcedistribution generator
Signed-off-by: dong <dong4325@126.com>
- Loading branch information
Showing
7 changed files
with
1,065 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
108
cmd/resourcedistributiongenerator/generator/resourcedistribution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.