Skip to content

Commit

Permalink
Merge pull request #853 from M0NsTeRRR/main
Browse files Browse the repository at this point in the history
feat: generate VIPCIDR if not value is set
  • Loading branch information
thebsdbox committed May 17, 2024
2 parents 1620e4c + 28918e8 commit 392583d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
54 changes: 54 additions & 0 deletions cmd/kube-vip-manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cmd

import (
"fmt"
"net"
"strings"

"github.com/kube-vip/kube-vip/pkg/kubevip"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -39,6 +41,8 @@ var kubeManifestPod = &cobra.Command{
Use: "pod",
Short: "Generate a Pod Manifest",
Run: func(cmd *cobra.Command, args []string) {
var err error

// Set the logging level for all subsequent functions
log.SetLevel(log.Level(logLevel))
initConfig.LoadBalancers = append(initConfig.LoadBalancers, initLoadBalancer)
Expand All @@ -53,6 +57,14 @@ var kubeManifestPod = &cobra.Command{
log.Fatalln("No address is specified for kube-vip to expose services on")
}

if initConfig.VIPCIDR == "" {
initConfig.VIPCIDR, err = generateCidrRange(initConfig.Address)

if err != nil {
log.Fatalln(err)
}
}

cfg := kubevip.GeneratePodManifestFromConfig(&initConfig, Release.Version, inCluster)
fmt.Println(cfg) // output manifest to stdout
},
Expand All @@ -62,6 +74,8 @@ var kubeManifestDaemon = &cobra.Command{
Use: "daemonset",
Short: "Generate a Daemonset Manifest",
Run: func(cmd *cobra.Command, args []string) {
var err error

// Set the logging level for all subsequent functions
log.SetLevel(log.Level(logLevel))
initConfig.LoadBalancers = append(initConfig.LoadBalancers, initLoadBalancer)
Expand All @@ -78,6 +92,14 @@ var kubeManifestDaemon = &cobra.Command{
log.Fatalln("No address is specified for kube-vip to expose services on")
}

if initConfig.VIPCIDR == "" {
initConfig.VIPCIDR, err = generateCidrRange(initConfig.Address)

if err != nil {
log.Fatalln(err)
}
}

cfg := kubevip.GenerateDaemonsetManifestFromConfig(&initConfig, Release.Version, inCluster, taint)
fmt.Println(cfg) // output manifest to stdout
},
Expand All @@ -87,6 +109,8 @@ var kubeManifestRbac = &cobra.Command{
Use: "rbac",
Short: "Generate an RBAC Manifest",
Run: func(cmd *cobra.Command, args []string) {
var err error

// Set the logging level for all subsequent functions
log.SetLevel(log.Level(logLevel))
initConfig.LoadBalancers = append(initConfig.LoadBalancers, initLoadBalancer)
Expand All @@ -100,8 +124,38 @@ var kubeManifestRbac = &cobra.Command{
_ = cmd.Help()
log.Fatalln("No address is specified for kube-vip to expose services on")
}

if initConfig.VIPCIDR == "" {
initConfig.VIPCIDR, err = generateCidrRange(initConfig.Address)

if err != nil {
log.Fatalln(err)
}
}

cfg := kubevip.GenerateSA()
b, _ := yaml.Marshal(cfg)
fmt.Println(string(b)) // output manifest to stdout
},
}

func generateCidrRange(address string) (string, error) {
var cidrs []string

addresses := strings.Split(address, ",")
for _, a := range addresses {
ip := net.ParseIP(a)

if ip == nil {
return "", fmt.Errorf("invalid IP address: %v", a)
}

if ip.To4() != nil {
cidrs = append(cidrs, "32")
} else {
cidrs = append(cidrs, "128")
}
}

return strings.Join(cidrs, ","), nil
}
2 changes: 1 addition & 1 deletion cmd/kube-vip.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func init() {
kubeVipCmd.PersistentFlags().StringVar(&initConfig.VIPSubnet, "vipSubnet", "", "The Virtual IP address subnet e.g. /32 /24 /8 etc..")
kubeVipCmd.PersistentFlags().StringVar(&initConfig.NodeName, "nodeName", "", "Name to be used for lease holder. Must be unique for each node/instance")

kubeVipCmd.PersistentFlags().StringVar(&initConfig.VIPCIDR, "cidr", "32", "The CIDR range for the virtual IP address") // todo: deprecate
kubeVipCmd.PersistentFlags().StringVar(&initConfig.VIPCIDR, "cidr", "", "The CIDR range for the virtual IP address. Default to 32 for IPv4 and 128 for IPv6") // todo: deprecate

kubeVipCmd.PersistentFlags().StringVar(&initConfig.Address, "address", "", "an address (IP or DNS name) to use as a VIP")
kubeVipCmd.PersistentFlags().IntVar(&initConfig.Port, "port", 6443, "Port for the VIP")
Expand Down

0 comments on commit 392583d

Please sign in to comment.