mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +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,
|
needFix: true,
|
||||||
createdSetters: []string{"cluster", "profile", "project"},
|
createdSetters: []string{"cluster", "profile", "project"},
|
||||||
createdSubst: []string{"subst-project-cluster"},
|
createdSubst: []string{"project-cluster-54235872"},
|
||||||
failedSetters: map[string]error{},
|
failedSetters: map[string]error{},
|
||||||
failedSubst: map[string]error{},
|
failedSubst: map[string]error{},
|
||||||
|
|
||||||
expectedOutput: `apiVersion: install.istio.io/v1alpha2
|
expectedOutput: `apiVersion: install.istio.io/v1alpha2
|
||||||
kind: IstioControlPlane
|
kind: IstioControlPlane
|
||||||
metadata:
|
metadata:
|
||||||
cluster: "someproj/someclus" # {"$openapi":"subst-project-cluster"}
|
cluster: "someproj/someclus" # {"$openapi":"project-cluster-54235872"}
|
||||||
spec:
|
spec:
|
||||||
profile: asm # {"$openapi":"profile"}
|
profile: asm # {"$openapi":"profile"}
|
||||||
cluster: "someproj/someclus" # {"$openapi":"subst-project-cluster"}
|
cluster: "someproj/someclus" # {"$openapi":"project-cluster-54235872"}
|
||||||
`,
|
`,
|
||||||
|
|
||||||
expectedOpenAPI: `apiVersion: kustomization.dev/v1alpha1
|
expectedOpenAPI: `apiVersion: kustomization.dev/v1alpha1
|
||||||
@@ -79,10 +79,10 @@ openAPI:
|
|||||||
setter:
|
setter:
|
||||||
name: project
|
name: project
|
||||||
value: someproj
|
value: someproj
|
||||||
io.k8s.cli.substitutions.subst-project-cluster:
|
io.k8s.cli.substitutions.project-cluster-54235872:
|
||||||
x-k8s-cli:
|
x-k8s-cli:
|
||||||
substitution:
|
substitution:
|
||||||
name: subst-project-cluster
|
name: project-cluster-54235872
|
||||||
pattern: ${project}/${cluster}
|
pattern: ${project}/${cluster}
|
||||||
values:
|
values:
|
||||||
- marker: ${project}
|
- marker: ${project}
|
||||||
@@ -105,7 +105,7 @@ spec:
|
|||||||
`,
|
`,
|
||||||
needFix: true,
|
needFix: true,
|
||||||
createdSetters: []string{"cluster", "profile", "project"},
|
createdSetters: []string{"cluster", "profile", "project"},
|
||||||
createdSubst: []string{"subst-project-cluster"},
|
createdSubst: []string{"project-cluster-54235872"},
|
||||||
failedSetters: map[string]error{},
|
failedSetters: map[string]error{},
|
||||||
failedSubst: map[string]error{},
|
failedSubst: map[string]error{},
|
||||||
|
|
||||||
@@ -135,7 +135,7 @@ kind: Kustomization`,
|
|||||||
|
|
||||||
needFix: true,
|
needFix: true,
|
||||||
createdSetters: []string{"profile", "team"},
|
createdSetters: []string{"profile", "team"},
|
||||||
createdSubst: []string{"subst-profile-team"},
|
createdSubst: []string{"profile-team-1851878264"},
|
||||||
failedSetters: map[string]error{},
|
failedSetters: map[string]error{},
|
||||||
failedSubst: map[string]error{},
|
failedSubst: map[string]error{},
|
||||||
|
|
||||||
@@ -144,7 +144,7 @@ kind: IstioControlPlane
|
|||||||
spec:
|
spec:
|
||||||
profile: asm # {"$openapi":"profile"}
|
profile: asm # {"$openapi":"profile"}
|
||||||
team: asm # {"$openapi":"team"}
|
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
|
expectedOpenAPI: `apiVersion: kustomization.dev/v1alpha1
|
||||||
@@ -163,10 +163,10 @@ openAPI:
|
|||||||
setter:
|
setter:
|
||||||
name: team
|
name: team
|
||||||
value: asm
|
value: asm
|
||||||
io.k8s.cli.substitutions.subst-profile-team:
|
io.k8s.cli.substitutions.profile-team-1851878264:
|
||||||
x-k8s-cli:
|
x-k8s-cli:
|
||||||
substitution:
|
substitution:
|
||||||
name: subst-profile-team
|
name: profile-team-1851878264
|
||||||
pattern: ${profile}/${team}
|
pattern: ${profile}/${team}
|
||||||
values:
|
values:
|
||||||
- marker: ${profile}
|
- 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",
|
name: "upgrade-with-both-versions",
|
||||||
|
|
||||||
@@ -388,6 +443,12 @@ spec:
|
|||||||
return
|
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 != "" {
|
if test.expectedOpenAPI != "" {
|
||||||
actualOpenAPI, err := ioutil.ReadFile(filepath.Join(dir, openAPIFileName))
|
actualOpenAPI, err := ioutil.ReadFile(filepath.Join(dir, openAPIFileName))
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
|
|||||||
@@ -4,8 +4,11 @@
|
|||||||
package fixsetters
|
package fixsetters
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"hash/fnv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -82,19 +85,25 @@ func (ls *upgradeV1Setters) lookupAndUpgrade(field *yaml.RNode) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(fm.Extensions.PartialFieldSetters) > 0 {
|
if len(fm.Extensions.PartialFieldSetters) > 0 {
|
||||||
substName := "subst"
|
fieldValue := field.YNode().Value
|
||||||
filedValue := field.YNode().Value
|
pattern := fieldValue
|
||||||
pattern := filedValue
|
|
||||||
|
|
||||||
|
var substName string
|
||||||
// derive substitution pattern from partial setters
|
// derive substitution pattern from partial setters
|
||||||
for i := range fm.Extensions.PartialFieldSetters {
|
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)
|
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{
|
ls.Substitutions = append(ls.Substitutions, substitution{
|
||||||
Name: substName,
|
Name: substName,
|
||||||
FieldVale: filedValue,
|
FieldVale: fieldValue,
|
||||||
Pattern: pattern,
|
Pattern: pattern,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -113,3 +122,13 @@ func (ls *upgradeV1Setters) lookupAndUpgrade(field *yaml.RNode) error {
|
|||||||
}
|
}
|
||||||
return nil
|
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 {
|
for _, subst := range substitutions {
|
||||||
l.Substitutions = append(l.Substitutions, *subst)
|
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 {
|
sort.Slice(l.SetterCounts, func(i, j int) bool {
|
||||||
return l.SetterCounts[i].Name < l.SetterCounts[j].Name
|
return l.SetterCounts[i].Name < l.SetterCounts[j].Name
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user