Skip to content

Commit

Permalink
Generate CNIGlobalConfig from GlobalConfig automatically (#260)
Browse files Browse the repository at this point in the history
Signed-off-by: Marko Lukša <mluksa@redhat.com>
  • Loading branch information
luksa authored Aug 12, 2024
1 parent f6b8fe7 commit 0a044f9
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 45 deletions.
32 changes: 32 additions & 0 deletions api/v1alpha1/values_types.gen.go

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

32 changes: 0 additions & 32 deletions api/v1alpha1/values_types_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@

package v1alpha1

import (
k8sv1 "k8s.io/api/core/v1"
)

type SDSConfigToken struct {
Aud string `json:"aud,omitempty"`
}
Expand All @@ -29,31 +25,3 @@ type CNIValues struct {
// Part of the global configuration applicable to the Istio CNI component.
Global *CNIGlobalConfig `json:"global,omitempty"`
}

// CNIGlobalConfig is a subset of the Global Configuration used in the Istio CNI chart.
type CNIGlobalConfig struct {
// Default k8s resources settings for all Istio control plane components.
//
// See https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/#resource-requests-and-limits-of-pod-and-container
DefaultResources *k8sv1.ResourceRequirements `json:"defaultResources,omitempty"`
// Specifies the docker hub for Istio images.
Hub string `json:"hub,omitempty"`
// Specifies the image pull policy for the Istio images. one of Always, Never, IfNotPresent.
// Defaults to Always if :latest tag is specified, or IfNotPresent otherwise. Cannot be updated.
//
// More info: https://kubernetes.io/docs/concepts/containers/images#updating-images
// +kubebuilder:validation:Enum=Always;Never;IfNotPresent
ImagePullPolicy k8sv1.PullPolicy `json:"imagePullPolicy,omitempty"`
// ImagePullSecrets for the control plane ServiceAccount, list of secrets in the same namespace
// to use for pulling any images in pods that reference this ServiceAccount.
// Must be set for any cluster configured with private docker registry.
ImagePullSecrets []string `json:"imagePullSecrets,omitempty"`
// Specifies whether istio components should output logs in json format by adding --log_as_json argument to each container.
LogAsJSON *bool `json:"logAsJson,omitempty"`
// Specifies the global logging level settings for the Istio CNI component.
Logging *GlobalLoggingConfig `json:"logging,omitempty"`
// Specifies the tag for the Istio CNI image.
Tag string `json:"tag,omitempty"`
// The variant of the Istio container images to use. Options are "debug" or "distroless". Unset will use the default for the given version.
Variant string `json:"variant,omitempty"`
}
9 changes: 7 additions & 2 deletions api/v1alpha1/zz_generated.deepcopy.go

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

8 changes: 4 additions & 4 deletions bundle/manifests/operator.istio.io_istiocnis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1299,10 +1299,10 @@ spec:
properties:
defaultResources:
description: |-
Default k8s resources settings for all Istio control plane components.
See https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/#resource-requests-and-limits-of-pod-and-container
See https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/#resource-requests-and-limits-of-pod-and-container
Deprecated: Marked as deprecated in pkg/apis/istio/v1alpha1/values_types.proto.
properties:
claims:
description: |-
Expand Down Expand Up @@ -1386,7 +1386,7 @@ spec:
type: boolean
logging:
description: Specifies the global logging level settings for
the Istio CNI component.
the Istio control plane components.
properties:
level:
description: |-
Expand All @@ -1396,7 +1396,7 @@ spec:
type: string
type: object
tag:
description: Specifies the tag for the Istio CNI image.
description: Specifies the tag for the Istio docker images.
type: string
variant:
description: The variant of the Istio container images to
Expand Down
8 changes: 4 additions & 4 deletions chart/crds/operator.istio.io_istiocnis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1299,10 +1299,10 @@ spec:
properties:
defaultResources:
description: |-
Default k8s resources settings for all Istio control plane components.
See https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/#resource-requests-and-limits-of-pod-and-container
See https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/#resource-requests-and-limits-of-pod-and-container
Deprecated: Marked as deprecated in pkg/apis/istio/v1alpha1/values_types.proto.
properties:
claims:
description: |-
Expand Down Expand Up @@ -1386,7 +1386,7 @@ spec:
type: boolean
logging:
description: Specifies the global logging level settings for
the Istio CNI component.
the Istio control plane components.
properties:
level:
description: |-
Expand All @@ -1396,7 +1396,7 @@ spec:
type: string
type: object
tag:
description: Specifies the tag for the Istio CNI image.
description: Specifies the tag for the Istio docker images.
type: string
variant:
description: The variant of the Istio container images to
Expand Down
6 changes: 3 additions & 3 deletions docs/api-reference/operator.istio.io.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ _Appears in:_

| Field | Description | Default | Validation |
| --- | --- | --- | --- |
| `defaultResources` _[ResourceRequirements](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#resourcerequirements-v1-core)_ | Default k8s resources settings for all Istio control plane components. See https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/#resource-requests-and-limits-of-pod-and-container | | |
| `defaultResources` _[ResourceRequirements](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#resourcerequirements-v1-core)_ | See https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/#resource-requests-and-limits-of-pod-and-container Deprecated: Marked as deprecated in pkg/apis/istio/v1alpha1/values_types.proto. | | |
| `hub` _string_ | Specifies the docker hub for Istio images. | | |
| `imagePullPolicy` _[PullPolicy](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#pullpolicy-v1-core)_ | Specifies the image pull policy for the Istio images. one of Always, Never, IfNotPresent. Defaults to Always if :latest tag is specified, or IfNotPresent otherwise. Cannot be updated. More info: https://kubernetes.io/docs/concepts/containers/images#updating-images | | Enum: [Always Never IfNotPresent] |
| `imagePullSecrets` _string array_ | ImagePullSecrets for the control plane ServiceAccount, list of secrets in the same namespace to use for pulling any images in pods that reference this ServiceAccount. Must be set for any cluster configured with private docker registry. | | |
| `logAsJson` _boolean_ | Specifies whether istio components should output logs in json format by adding --log_as_json argument to each container. | | |
| `logging` _[GlobalLoggingConfig](#globalloggingconfig)_ | Specifies the global logging level settings for the Istio CNI component. | | |
| `tag` _string_ | Specifies the tag for the Istio CNI image. | | |
| `logging` _[GlobalLoggingConfig](#globalloggingconfig)_ | Specifies the global logging level settings for the Istio control plane components. | | |
| `tag` _string_ | Specifies the tag for the Istio docker images. | | |
| `variant` _string_ | The variant of the Istio container images to use. Options are "debug" or "distroless". Unset will use the default for the given version. | | |


Expand Down
46 changes: 46 additions & 0 deletions hack/api_transformer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"path/filepath"
"regexp"
"slices"
"strings"

"golang.org/x/mod/modfile"
Expand Down Expand Up @@ -70,9 +71,17 @@ type Transformations struct {
ReplaceFunctionReturnTypes map[string]string `yaml:"replaceFunctionReturnTypes"`
ReplaceFieldTypes map[string]string `yaml:"replaceFieldTypes"`
ReplaceTypes map[string]string `yaml:"replaceTypes"`
CopyTypes []CopyTransform `yaml:"copyTypes"`
AddComments map[string]string `yaml:"addComments"`
}

type CopyTransform struct {
From string `yaml:"from"`
To string `yaml:"to"`
Comments []string `yaml:"comments"`
IncludeFields []string `yaml:"includeFields"`
}

var config *Config

type FileTransformer struct {
Expand Down Expand Up @@ -139,6 +148,7 @@ func merge(local, global *Transformations) *Transformations {
ReplaceFunctionReturnTypes: mergeStringMaps(local.ReplaceFunctionReturnTypes, global.ReplaceFunctionReturnTypes),
ReplaceFieldTypes: mergeStringMaps(local.ReplaceFieldTypes, global.ReplaceFieldTypes),
ReplaceTypes: mergeStringMaps(local.ReplaceTypes, global.ReplaceTypes),
CopyTypes: local.CopyTypes,
AddComments: mergeStringMaps(local.AddComments, global.AddComments),
}
}
Expand Down Expand Up @@ -265,6 +275,33 @@ func (t *FileTransformer) processFile() (*ast.File, error) {
if newName := t.getTypeRename(structName); newName != "" {
typeSpec.Name.Name = newName
}

if ct, found := t.getCopyTransform(structName); found {
structCopy := &ast.StructType{
Fields: &ast.FieldList{},
}
for _, f := range structType.Fields.List {
if slices.Contains(ct.IncludeFields, f.Names[0].Name) {
structCopy.Fields.List = append(structCopy.Fields.List, f)
}
}
declCopy := &ast.GenDecl{
Tok: token.TYPE,
Specs: []ast.Spec{
&ast.TypeSpec{
Name: &ast.Ident{Name: ct.To},
Type: structCopy,
},
},
}
if len(ct.Comments) > 0 {
declCopy.Doc = &ast.CommentGroup{}
for _, c := range ct.Comments {
declCopy.Doc.List = append(declCopy.Doc.List, &ast.Comment{Text: c})
}
}
file.Decls = append(file.Decls, declCopy)
}
}
}
return true
Expand Down Expand Up @@ -770,6 +807,15 @@ func matches(parent string, child string, list []string) bool {
return false
}

func (t *FileTransformer) getCopyTransform(typeName string) (CopyTransform, bool) {
for _, ct := range t.Transformations.CopyTypes {
if ct.From == typeName {
return ct, true
}
}
return CopyTransform{}, false
}

func (t *FileTransformer) getTypeRename(typeName string) string {
return t.Transformations.RenameTypes[typeName]
}
Expand Down
14 changes: 14 additions & 0 deletions hack/api_transformer/transform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ inputFiles:
SidecarInjectorConfig.AlwaysInjectSelector: "[]metav1.LabelSelector"
SidecarInjectorConfig.InjectedAnnotations: "map[string]string"
SidecarInjectorConfig.Templates: "map[string]string"
copyTypes:
- from: GlobalConfig
to: CNIGlobalConfig
comments:
- // CNIGlobalConfig is a subset of the Global Configuration used in the Istio CNI chart.
includeFields:
- DefaultResources
- Hub
- ImagePullPolicy
- ImagePullSecrets
- LogAsJson
- Logging
- Tag
- Variant
addComments:
CNIConfig.PullPolicy: "// +kubebuilder:validation:Enum=Always;Never;IfNotPresent"
GlobalConfig.ImagePullPolicy: "// +kubebuilder:validation:Enum=Always;Never;IfNotPresent"
Expand Down

0 comments on commit 0a044f9

Please sign in to comment.