Skip to content

Commit

Permalink
Merge pull request #9410 from johngmyers/refactor-lyft
Browse files Browse the repository at this point in the history
Refactor lyft config file to Go code
  • Loading branch information
k8s-ci-robot authored Jun 20, 2020
2 parents 9e62de5 + 87a9810 commit 10553e1
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 360 deletions.
1 change: 0 additions & 1 deletion cmd/nodeup/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ go_library(
deps = [
"//:go_default_library",
"//nodeup/pkg/bootstrap:go_default_library",
"//upup/models:go_default_library",
"//upup/pkg/fi/nodeup:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
],
Expand Down
2 changes: 0 additions & 2 deletions cmd/nodeup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"k8s.io/klog"
"k8s.io/kops"
"k8s.io/kops/nodeup/pkg/bootstrap"
"k8s.io/kops/upup/models"
"k8s.io/kops/upup/pkg/fi/nodeup"
)

Expand Down Expand Up @@ -117,7 +116,6 @@ func main() {
Target: target,
CacheDir: flagCacheDir,
FSRoot: flagRootFS,
ModelDir: models.NewAssetPath("nodeup"),
}
err = cmd.Run(os.Stdout)
if err == nil {
Expand Down
88 changes: 55 additions & 33 deletions nodeup/pkg/model/networking/lyft.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import (
"encoding/json"
"fmt"
"strings"
"text/template"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"k8s.io/kops/upup/pkg/fi/nodeup/nodetasks"

"k8s.io/klog"
"k8s.io/kops/nodeup/pkg/model"
Expand Down Expand Up @@ -59,45 +59,67 @@ func (b *LyftVPCBuilder) Build(c *fi.ModelBuilderContext) error {
return err
}

return nil
}
securityGroups, err := evaluateSecurityGroups(b.Cluster.Spec.NetworkID)
if err != nil {
return err
}

func LoadLyftTemplateFunctions(templateFunctions template.FuncMap, cluster *api.Cluster) {
templateFunctions["SubnetTags"] = func() (string, error) {
var tags map[string]string
if cluster.IsKubernetesGTE("1.18") {
tags = map[string]string{
"KubernetesCluster": cluster.Name,
}
} else {
tags = map[string]string{
"Type": "pod",
}
}
if len(cluster.Spec.Networking.LyftVPC.SubnetTags) > 0 {
tags = cluster.Spec.Networking.LyftVPC.SubnetTags
}
conflist := map[string]interface{}{
"cniVersion": "0.3.1",
"name": "cni-ipvlan-vpc-k8s",
"plugins": []map[string]interface{}{
{
"cniVersion": "0.3.1",
"type": "cni-ipvlan-vpc-k8s-ipam",
"interfaceIndex": 1,
"skipDeallocation": true,
"subnetTags": getSubnetTags(b.Cluster),
"secGroupIds": securityGroups,
},
{
"cniVersion": "0.3.1",
"type": "cni-ipvlan-vpc-k8s-ipvlan",
"mode": "l2",
},
{
"cniVersion": "0.3.1",
"type": "cni-ipvlan-vpc-k8s-unnumbered-ptp",
"hostInterface": "eth0",
"containerInterface": "veth0",
"ipMasq": true,
},
},
}

bytes, err := json.Marshal(tags)
if err != nil {
return "", err
}
return string(bytes), nil
bytes, err := json.Marshal(conflist)
if err != nil {
return err
}

templateFunctions["NodeSecurityGroups"] = func() (string, error) {
// use the same security groups as the node
ids, err := evaluateSecurityGroups(cluster.Spec.NetworkID)
if err != nil {
return "", err
c.AddTask(&nodetasks.File{
Contents: fi.NewBytesResource(bytes),
Path: "/etc/cni/net.d/10-cni-ipvlan-vpc-k8s.conflist",
Type: nodetasks.FileType_File,
})

return nil
}

func getSubnetTags(cluster *api.Cluster) interface{} {
var tags map[string]string
if cluster.IsKubernetesGTE("1.18") {
tags = map[string]string{
"KubernetesCluster": cluster.Name,
}
bytes, err := json.Marshal(ids)
if err != nil {
return "", err
} else {
tags = map[string]string{
"Type": "pod",
}
return string(bytes), nil
}

if len(cluster.Spec.Networking.LyftVPC.SubnetTags) > 0 {
tags = cluster.Spec.Networking.LyftVPC.SubnetTags
}
return tags
}

func evaluateSecurityGroups(vpcId string) ([]string, error) {
Expand Down
60 changes: 0 additions & 60 deletions upup/models/bindata.go

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

This file was deleted.

4 changes: 0 additions & 4 deletions upup/pkg/fi/cloudup/tagbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ func buildNodeupTags(role api.InstanceGroupRole, cluster *api.Cluster, clusterTa
return nil, fmt.Errorf("Networking is not set, and should not be nil here")
}

if networking.LyftVPC != nil {
tags.Insert("_lyft_vpc_cni")
}

switch fi.StringValue(cluster.Spec.UpdatePolicy) {
case "": // default
tags.Insert("_automatic_upgrades")
Expand Down
1 change: 0 additions & 1 deletion upup/pkg/fi/nodeup/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ go_library(
"//pkg/apis/nodeup:go_default_library",
"//pkg/assets:go_default_library",
"//upup/pkg/fi:go_default_library",
"//upup/pkg/fi/loader:go_default_library",
"//upup/pkg/fi/nodeup/cloudinit:go_default_library",
"//upup/pkg/fi/nodeup/local:go_default_library",
"//upup/pkg/fi/nodeup/nodetasks:go_default_library",
Expand Down
7 changes: 2 additions & 5 deletions upup/pkg/fi/nodeup/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ type NodeUpCommand struct {
CacheDir string
ConfigLocation string
FSRoot string
ModelDir vfs.Path
Target string
cluster *api.Cluster
config *nodeup.Config
Expand Down Expand Up @@ -237,7 +236,7 @@ func (c *NodeUpCommand) Run(out io.Writer) error {
return err
}

loader := NewLoader(c.config, c.cluster, assetStore, nodeTags)
loader := &Loader{}
loader.Builders = append(loader.Builders, &model.NTPBuilder{NodeupModelContext: modelContext})
loader.Builders = append(loader.Builders, &model.MiscUtilsBuilder{NodeupModelContext: modelContext})
loader.Builders = append(loader.Builders, &model.DirectoryBuilder{NodeupModelContext: modelContext})
Expand Down Expand Up @@ -273,9 +272,7 @@ func (c *NodeUpCommand) Run(out io.Writer) error {
loader.Builders = append(loader.Builders, &networking.KuberouterBuilder{NodeupModelContext: modelContext})
loader.Builders = append(loader.Builders, &networking.LyftVPCBuilder{NodeupModelContext: modelContext})

networking.LoadLyftTemplateFunctions(loader.TemplateFunctions, c.cluster)

taskMap, err := loader.Build(c.ModelDir)
taskMap, err := loader.Build()
if err != nil {
return fmt.Errorf("error building loader: %v", err)
}
Expand Down
Loading

0 comments on commit 10553e1

Please sign in to comment.