mirror of https://github.com/kubernetes/kops.git
Refactor Literal sorting
This commit is contained in:
parent
70fe547deb
commit
7ad81f9965
|
@ -1,11 +1,11 @@
|
|||
locals {
|
||||
cluster_name = "complex.example.com"
|
||||
master_autoscaling_group_ids = [aws_autoscaling_group.master-us-test-1a-masters-complex-example-com.id]
|
||||
master_security_group_ids = [aws_security_group.masters-complex-example-com.id, "sg-exampleid5", "sg-exampleid6"]
|
||||
master_security_group_ids = ["sg-exampleid5", "sg-exampleid6", aws_security_group.masters-complex-example-com.id]
|
||||
masters_role_arn = aws_iam_role.masters-complex-example-com.arn
|
||||
masters_role_name = aws_iam_role.masters-complex-example-com.name
|
||||
node_autoscaling_group_ids = [aws_autoscaling_group.nodes-complex-example-com.id]
|
||||
node_security_group_ids = [aws_security_group.nodes-complex-example-com.id, "sg-exampleid3", "sg-exampleid4"]
|
||||
node_security_group_ids = ["sg-exampleid3", "sg-exampleid4", aws_security_group.nodes-complex-example-com.id]
|
||||
node_subnet_ids = [aws_subnet.us-test-1a-complex-example-com.id]
|
||||
nodes_role_arn = aws_iam_role.nodes-complex-example-com.arn
|
||||
nodes_role_name = aws_iam_role.nodes-complex-example-com.name
|
||||
|
@ -30,7 +30,7 @@ output "master_autoscaling_group_ids" {
|
|||
}
|
||||
|
||||
output "master_security_group_ids" {
|
||||
value = [aws_security_group.masters-complex-example-com.id, "sg-exampleid5", "sg-exampleid6"]
|
||||
value = ["sg-exampleid5", "sg-exampleid6", aws_security_group.masters-complex-example-com.id]
|
||||
}
|
||||
|
||||
output "masters_role_arn" {
|
||||
|
@ -46,7 +46,7 @@ output "node_autoscaling_group_ids" {
|
|||
}
|
||||
|
||||
output "node_security_group_ids" {
|
||||
value = [aws_security_group.nodes-complex-example-com.id, "sg-exampleid3", "sg-exampleid4"]
|
||||
value = ["sg-exampleid3", "sg-exampleid4", aws_security_group.nodes-complex-example-com.id]
|
||||
}
|
||||
|
||||
output "node_subnet_ids" {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
locals {
|
||||
cluster_name = "existingsg.example.com"
|
||||
master_autoscaling_group_ids = [aws_autoscaling_group.master-us-test-1a-masters-existingsg-example-com.id, aws_autoscaling_group.master-us-test-1b-masters-existingsg-example-com.id, aws_autoscaling_group.master-us-test-1c-masters-existingsg-example-com.id]
|
||||
master_security_group_ids = [aws_security_group.masters-existingsg-example-com.id, "sg-master-1a", "sg-master-1b"]
|
||||
master_security_group_ids = ["sg-master-1a", "sg-master-1b", aws_security_group.masters-existingsg-example-com.id]
|
||||
masters_role_arn = aws_iam_role.masters-existingsg-example-com.arn
|
||||
masters_role_name = aws_iam_role.masters-existingsg-example-com.name
|
||||
node_autoscaling_group_ids = [aws_autoscaling_group.nodes-existingsg-example-com.id]
|
||||
|
@ -29,7 +29,7 @@ output "master_autoscaling_group_ids" {
|
|||
}
|
||||
|
||||
output "master_security_group_ids" {
|
||||
value = [aws_security_group.masters-existingsg-example-com.id, "sg-master-1a", "sg-master-1b"]
|
||||
value = ["sg-master-1a", "sg-master-1b", aws_security_group.masters-existingsg-example-com.id]
|
||||
}
|
||||
|
||||
output "masters_role_arn" {
|
||||
|
|
|
@ -5,7 +5,7 @@ locals {
|
|||
masters_role_arn = aws_iam_role.masters-externalpolicies-example-com.arn
|
||||
masters_role_name = aws_iam_role.masters-externalpolicies-example-com.name
|
||||
node_autoscaling_group_ids = [aws_autoscaling_group.nodes-externalpolicies-example-com.id]
|
||||
node_security_group_ids = [aws_security_group.nodes-externalpolicies-example-com.id, "sg-exampleid3", "sg-exampleid4"]
|
||||
node_security_group_ids = ["sg-exampleid3", "sg-exampleid4", aws_security_group.nodes-externalpolicies-example-com.id]
|
||||
node_subnet_ids = [aws_subnet.us-test-1a-externalpolicies-example-com.id]
|
||||
nodes_role_arn = aws_iam_role.nodes-externalpolicies-example-com.arn
|
||||
nodes_role_name = aws_iam_role.nodes-externalpolicies-example-com.name
|
||||
|
@ -43,7 +43,7 @@ output "node_autoscaling_group_ids" {
|
|||
}
|
||||
|
||||
output "node_security_group_ids" {
|
||||
value = [aws_security_group.nodes-externalpolicies-example-com.id, "sg-exampleid3", "sg-exampleid4"]
|
||||
value = ["sg-exampleid3", "sg-exampleid4", aws_security_group.nodes-externalpolicies-example-com.id]
|
||||
}
|
||||
|
||||
output "node_subnet_ids" {
|
||||
|
@ -294,7 +294,7 @@ resource "aws_elb" "api-externalpolicies-example-com" {
|
|||
lb_protocol = "TCP"
|
||||
}
|
||||
name = "api-externalpolicies-exam-5cse45"
|
||||
security_groups = [aws_security_group.api-elb-externalpolicies-example-com.id, "sg-exampleid3", "sg-exampleid4"]
|
||||
security_groups = ["sg-exampleid3", "sg-exampleid4", aws_security_group.api-elb-externalpolicies-example-com.id]
|
||||
subnets = [aws_subnet.us-test-1a-externalpolicies-example-com.id]
|
||||
tags = {
|
||||
"KubernetesCluster" = "externalpolicies.example.com"
|
||||
|
|
|
@ -21,15 +21,13 @@ import (
|
|||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
// Literal represents a literal in terraform syntax
|
||||
type Literal struct {
|
||||
// String is the Terraform representation.
|
||||
String string `cty:"string"`
|
||||
// Value is used to support JSON marshalling for unit tests and sorting.
|
||||
// Value is used to support JSON marshalling for unit tests.
|
||||
Value string `cty:"value"`
|
||||
|
||||
// FnArgs contains string representations of arguments to the function call.
|
||||
|
@ -95,49 +93,11 @@ func LiteralWithIndex(s string) *Literal {
|
|||
}
|
||||
}
|
||||
|
||||
type literalWithJSON struct {
|
||||
literal *Literal
|
||||
key string
|
||||
}
|
||||
|
||||
type byKey []*literalWithJSON
|
||||
|
||||
func (a byKey) Len() int { return len(a) }
|
||||
func (a byKey) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a byKey) Less(i, j int) bool {
|
||||
return a[i].key < a[j].key
|
||||
}
|
||||
|
||||
// buildSortProxies maps a list of Literals to a list of literalWithJSON
|
||||
func buildSortProxies(v []*Literal) ([]*literalWithJSON, error) {
|
||||
var proxies []*literalWithJSON
|
||||
for _, l := range v {
|
||||
k, err := json.Marshal(l)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
proxies = append(proxies, &literalWithJSON{
|
||||
literal: l,
|
||||
key: string(k),
|
||||
})
|
||||
}
|
||||
|
||||
return proxies, nil
|
||||
}
|
||||
|
||||
// SortLiterals sorts a list of Literal, by key. It does so in-place
|
||||
func SortLiterals(v []*Literal) {
|
||||
proxies, err := buildSortProxies(v)
|
||||
if err != nil {
|
||||
// Very unexpected
|
||||
klog.Fatalf("error processing terraform Literal: %v", err)
|
||||
}
|
||||
|
||||
sort.Sort(byKey(proxies))
|
||||
|
||||
for i := range proxies {
|
||||
v[i] = proxies[i].literal
|
||||
}
|
||||
sort.Slice(v, func(i, j int) bool {
|
||||
return v[i].String < v[j].String
|
||||
})
|
||||
}
|
||||
|
||||
// dedupLiterals removes any duplicate Literals before returning the slice.
|
||||
|
@ -147,19 +107,14 @@ func dedupLiterals(v []*Literal) ([]*Literal, error) {
|
|||
return nil, nil
|
||||
}
|
||||
|
||||
proxies, err := buildSortProxies(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sort.Sort(byKey(proxies))
|
||||
SortLiterals(v)
|
||||
|
||||
var deduped []*Literal
|
||||
for i, p := range proxies {
|
||||
if i != 0 && proxies[i-1].key == proxies[i].key {
|
||||
for i, p := range v {
|
||||
if i != 0 && v[i-1].String == v[i].String {
|
||||
continue
|
||||
}
|
||||
deduped = append(deduped, p.literal)
|
||||
deduped = append(deduped, p)
|
||||
}
|
||||
|
||||
return deduped, nil
|
||||
|
|
Loading…
Reference in New Issue