mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-10 08:20:59 +00:00
Merge pull request #2797 from phanimarupaka/SuffixSubstInFix
Infix/Suffix subst creation for fix
This commit is contained in:
@@ -44,17 +44,17 @@ kind: Kustomization`,
|
||||
|
||||
needFix: true,
|
||||
createdSetters: []string{"cluster", "profile", "project"},
|
||||
createdSubst: []string{"subst-project-cluster"},
|
||||
createdSubst: []string{"project-cluster-54235872"},
|
||||
failedSetters: map[string]error{},
|
||||
failedSubst: map[string]error{},
|
||||
|
||||
expectedOutput: `apiVersion: install.istio.io/v1alpha2
|
||||
kind: IstioControlPlane
|
||||
metadata:
|
||||
cluster: "someproj/someclus" # {"$openapi":"subst-project-cluster"}
|
||||
cluster: "someproj/someclus" # {"$openapi":"project-cluster-54235872"}
|
||||
spec:
|
||||
profile: asm # {"$openapi":"profile"}
|
||||
cluster: "someproj/someclus" # {"$openapi":"subst-project-cluster"}
|
||||
cluster: "someproj/someclus" # {"$openapi":"project-cluster-54235872"}
|
||||
`,
|
||||
|
||||
expectedOpenAPI: `apiVersion: kustomization.dev/v1alpha1
|
||||
@@ -79,10 +79,10 @@ openAPI:
|
||||
setter:
|
||||
name: project
|
||||
value: someproj
|
||||
io.k8s.cli.substitutions.subst-project-cluster:
|
||||
io.k8s.cli.substitutions.project-cluster-54235872:
|
||||
x-k8s-cli:
|
||||
substitution:
|
||||
name: subst-project-cluster
|
||||
name: project-cluster-54235872
|
||||
pattern: ${project}/${cluster}
|
||||
values:
|
||||
- marker: ${project}
|
||||
@@ -105,7 +105,7 @@ spec:
|
||||
`,
|
||||
needFix: true,
|
||||
createdSetters: []string{"cluster", "profile", "project"},
|
||||
createdSubst: []string{"subst-project-cluster"},
|
||||
createdSubst: []string{"project-cluster-54235872"},
|
||||
failedSetters: map[string]error{},
|
||||
failedSubst: map[string]error{},
|
||||
|
||||
@@ -135,7 +135,7 @@ kind: Kustomization`,
|
||||
|
||||
needFix: true,
|
||||
createdSetters: []string{"profile", "team"},
|
||||
createdSubst: []string{"subst-profile-team"},
|
||||
createdSubst: []string{"profile-team-1851878264"},
|
||||
failedSetters: map[string]error{},
|
||||
failedSubst: map[string]error{},
|
||||
|
||||
@@ -144,7 +144,7 @@ kind: IstioControlPlane
|
||||
spec:
|
||||
profile: asm # {"$openapi":"profile"}
|
||||
team: asm # {"$openapi":"team"}
|
||||
profile-team: asm/asm # {"$openapi":"subst-profile-team"}
|
||||
profile-team: asm/asm # {"$openapi":"profile-team-1851878264"}
|
||||
`,
|
||||
|
||||
expectedOpenAPI: `apiVersion: kustomization.dev/v1alpha1
|
||||
@@ -163,10 +163,10 @@ openAPI:
|
||||
setter:
|
||||
name: team
|
||||
value: asm
|
||||
io.k8s.cli.substitutions.subst-profile-team:
|
||||
io.k8s.cli.substitutions.profile-team-1851878264:
|
||||
x-k8s-cli:
|
||||
substitution:
|
||||
name: subst-profile-team
|
||||
name: profile-team-1851878264
|
||||
pattern: ${profile}/${team}
|
||||
values:
|
||||
- marker: ${profile}
|
||||
@@ -176,6 +176,61 @@ openAPI:
|
||||
`,
|
||||
},
|
||||
|
||||
{
|
||||
name: "partial-setters-suffix-subst",
|
||||
input: `
|
||||
apiVersion: install.istio.io/v1alpha2
|
||||
kind: IstioControlPlane
|
||||
spec:
|
||||
profile: asm-profile # {"type":"string","x-kustomize":{"partialSetters":[{"name":"asm","value":"asm"}]}}
|
||||
team: asm-team # {"type":"string","x-kustomize":{"partialSetters":[{"name":"asm","value":"asm"}]}}
|
||||
`,
|
||||
|
||||
openAPIFile: `apiVersion: kustomization.dev/v1alpha1
|
||||
kind: Kustomization`,
|
||||
|
||||
needFix: true,
|
||||
createdSetters: []string{"asm"},
|
||||
createdSubst: []string{"asm-3472570278", "asm-3647054792"},
|
||||
failedSetters: map[string]error{},
|
||||
failedSubst: map[string]error{},
|
||||
|
||||
expectedOutput: `apiVersion: install.istio.io/v1alpha2
|
||||
kind: IstioControlPlane
|
||||
spec:
|
||||
profile: asm-profile # {"$openapi":"asm-3647054792"}
|
||||
team: asm-team # {"$openapi":"asm-3472570278"}
|
||||
`,
|
||||
|
||||
expectedOpenAPI: `apiVersion: kustomization.dev/v1alpha1
|
||||
kind: Kustomization
|
||||
openAPI:
|
||||
definitions:
|
||||
io.k8s.cli.setters.asm:
|
||||
type: string
|
||||
x-k8s-cli:
|
||||
setter:
|
||||
name: asm
|
||||
value: asm
|
||||
io.k8s.cli.substitutions.asm-3472570278:
|
||||
x-k8s-cli:
|
||||
substitution:
|
||||
name: asm-3472570278
|
||||
pattern: ${asm}-team
|
||||
values:
|
||||
- marker: ${asm}
|
||||
ref: '#/definitions/io.k8s.cli.setters.asm'
|
||||
io.k8s.cli.substitutions.asm-3647054792:
|
||||
x-k8s-cli:
|
||||
substitution:
|
||||
name: asm-3647054792
|
||||
pattern: ${asm}-profile
|
||||
values:
|
||||
- marker: ${asm}
|
||||
ref: '#/definitions/io.k8s.cli.setters.asm'
|
||||
`,
|
||||
},
|
||||
|
||||
{
|
||||
name: "upgrade-with-both-versions",
|
||||
|
||||
@@ -388,6 +443,12 @@ spec:
|
||||
return
|
||||
}
|
||||
|
||||
actualOutput, err := ioutil.ReadFile(filepath.Join(dir, "deploy.yaml"))
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
assert.Equal(t, test.expectedOutput, string(actualOutput))
|
||||
|
||||
if test.expectedOpenAPI != "" {
|
||||
actualOpenAPI, err := ioutil.ReadFile(filepath.Join(dir, openAPIFileName))
|
||||
if !assert.NoError(t, err) {
|
||||
|
||||
@@ -4,8 +4,11 @@
|
||||
package fixsetters
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
@@ -82,19 +85,25 @@ func (ls *upgradeV1Setters) lookupAndUpgrade(field *yaml.RNode) error {
|
||||
}
|
||||
|
||||
if len(fm.Extensions.PartialFieldSetters) > 0 {
|
||||
substName := "subst"
|
||||
filedValue := field.YNode().Value
|
||||
pattern := filedValue
|
||||
fieldValue := field.YNode().Value
|
||||
pattern := fieldValue
|
||||
|
||||
var substName string
|
||||
// derive substitution pattern from partial setters
|
||||
for i := range fm.Extensions.PartialFieldSetters {
|
||||
substName += "-" + fm.Extensions.PartialFieldSetters[i].Name
|
||||
substName += fm.Extensions.PartialFieldSetters[i].Name + "-"
|
||||
pattern = strings.Replace(pattern, fm.Extensions.PartialFieldSetters[i].Value, `${`+fm.Extensions.PartialFieldSetters[i].Name+"}", 1)
|
||||
}
|
||||
|
||||
fvHash, err := FNV32aHash(fieldValue)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
substName += fvHash
|
||||
ls.Substitutions = append(ls.Substitutions, substitution{
|
||||
Name: substName,
|
||||
FieldVale: filedValue,
|
||||
FieldVale: fieldValue,
|
||||
Pattern: pattern,
|
||||
})
|
||||
}
|
||||
@@ -113,3 +122,13 @@ func (ls *upgradeV1Setters) lookupAndUpgrade(field *yaml.RNode) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FNV32aHash generates 32-bit FNV-1a hash for input string
|
||||
func FNV32aHash(text string) (string, error) {
|
||||
algorithm := fnv.New32a()
|
||||
_, err := algorithm.Write([]byte(text))
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err)
|
||||
}
|
||||
return fmt.Sprint(algorithm.Sum32()), nil
|
||||
}
|
||||
|
||||
@@ -74,6 +74,11 @@ func (l *UpgradeV1Setters) Filter(input []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
for _, subst := range substitutions {
|
||||
l.Substitutions = append(l.Substitutions, *subst)
|
||||
}
|
||||
|
||||
sort.Slice(l.Substitutions, func(i, j int) bool {
|
||||
return l.Substitutions[i].Name < l.Substitutions[j].Name
|
||||
})
|
||||
|
||||
sort.Slice(l.SetterCounts, func(i, j int) bool {
|
||||
return l.SetterCounts[i].Name < l.SetterCounts[j].Name
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user