Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions cmd/machine-provisioner/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
Copyright 2022 The Machine Controller Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"context"
"encoding/json"
"errors"
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

clusterv1alpha1 "github.com/kubermatic/machine-controller/pkg/apis/cluster/v1alpha1"
"github.com/kubermatic/machine-controller/pkg/provisioner"

"sigs.k8s.io/yaml"
)

func newCreateCommand(rootFlags *pflag.FlagSet) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Create a machine",
Long: "",
Args: cobra.ExactArgs(0),
SilenceErrors: true,
Example: `machine-provisioner create --machine-config ./machines.yaml`,
Run: func(_ *cobra.Command, _ []string) {
err := runCreateMachineCommand(opts.MachineConfig)
if err != nil {
logrus.Fatal(err)
}
},
}

return cmd
}

func runCreateMachineCommand(machineConfigFile string) error {
logrus.Info("Running command to create machines")

if len(machineConfigFile) == 0 {
return errors.New("machine configuration path is empty")
}

machineConfig, err := os.ReadFile(machineConfigFile)
if err != nil {
return errors.New("failed to read machine configuration")
}

machines, err := parseYAMLToObjects(machineConfig)
if err != nil {
return err
}

out, err := provisioner.CreateMachines(context.Background(), machines)
if err != nil {
return err
}

b, err := json.MarshalIndent(out, "", " ")
if err != nil {
return err
}

err = os.WriteFile("machines.json", b, 0600)
if err != nil {
return err
}

logrus.Infof("Create task ran successfully. Output is available in %q.", provisioner.OutputFileName)
return nil
}

func parseYAMLToObjects(machineByte []byte) ([]clusterv1alpha1.Machine, error) {
machine := []clusterv1alpha1.Machine{}
if err := yaml.Unmarshal(machineByte, &machine); err != nil {
return nil, err
}

return machine, nil
}
83 changes: 83 additions & 0 deletions cmd/machine-provisioner/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright 2022 The Machine Controller Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"fmt"
"os"
"path/filepath"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

type options struct {
LogFormat string
MachineConfig string
}

var opts options

func main() {
rootCmd := newRootCmd()
if err := rootCmd.Execute(); err != nil {
logrus.Fatalf("Error executing machine-provisioner: %v", err)
}
}

func newRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: filepath.Base(os.Args[0]),
Short: "Tool to provision machines",
Long: "Tool to provision machines on various cloud providers.",
PersistentPreRun: runRootCmd,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, _ []string) error {
return cmd.Help()
},
}

// Options
cmd.PersistentFlags().StringVar(&opts.LogFormat, "log-format", "", "Log format to use (empty string for text, or JSON")
cmd.PersistentFlags().StringVar(&opts.MachineConfig, "machine-config", "./machines.yaml", "Path to the YAML file for machines")

cmd.AddCommand(newCreateCommand(cmd.PersistentFlags()))

return cmd
}

func runRootCmd(cmd *cobra.Command, args []string) {
err := configureLogging(opts.LogFormat)
if err != nil {
logrus.Warn(err)
}
}

func configureLogging(logFormat string) error {
logrus.SetLevel(logrus.DebugLevel)

switch logFormat {
case "json":
logrus.SetFormatter(&logrus.JSONFormatter{})
default:
// just let the library use default on empty string.
if logFormat != "" {
return fmt.Errorf("unsupported logging formatter: %q", logFormat)
}
}
return nil
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ require (
github.com/prometheus/client_golang v1.12.2
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9
github.com/sethvargo/go-password v0.2.0
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
github.com/tinkerbell/tink v0.0.0-20210315140655-1b178daeaeda
github.com/vmware/go-vcloud-director/v2 v2.15.0
github.com/vmware/govmomi v0.28.0
Expand Down Expand Up @@ -113,6 +116,7 @@ require (
github.com/hashicorp/go-version v1.2.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand All @@ -136,7 +140,6 @@ require (
github.com/rogpeppe/go-internal v1.6.1 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/vincent-petithory/dataurl v1.0.0 // indirect
go.opencensus.io v0.23.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJ
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
Expand Down Expand Up @@ -806,6 +807,7 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
Expand All @@ -824,6 +826,7 @@ github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/cobra v1.0.1-0.20200713175500-884edc58ad08/go.mod h1:yk5b0mALVusDL5fMM6Rd1wgnoO5jUPhwsQ6LQAJTidQ=
github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudprovider/provider/anexia/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"testing"

"github.com/gophercloud/gophercloud/testhelper"

"go.anx.io/go-anxcloud/pkg/vsphere/info"

v1 "k8s.io/api/core/v1"
)

Expand Down
1 change: 0 additions & 1 deletion pkg/cloudprovider/provider/aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/aws/smithy-go"

gocache "github.com/patrickmn/go-cache"
"github.com/prometheus/client_golang/prometheus"

Expand Down
7 changes: 4 additions & 3 deletions pkg/cloudprovider/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ type MachineUpdater func(*clusterv1alpha1.Machine, ...MachineModifier) error

// ProviderData is the struct the cloud providers get when creating or deleting an instance.
type ProviderData struct {
Ctx context.Context
Update MachineUpdater
Client ctrlruntimeclient.Client
Ctx context.Context
Update MachineUpdater
Client ctrlruntimeclient.Client
ProvisionerMode bool
}

// GetMachineUpdater returns an MachineUpdater based on the passed in context and ctrlruntimeclient.Client.
Expand Down
80 changes: 80 additions & 0 deletions pkg/provisioner/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright 2022 The Machine Controller Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package provisioner

import (
v1 "k8s.io/api/core/v1"
)

const OutputFileName = "machines.json"

type output struct {
Machines []machine `json:"machines"`
}

type machine struct {
PublicAddress string `json:"public_address,omitempty"`
PrivateAddress string `json:"private_address,omitempty"`
Hostname string `json:"hostname,omitempty"`
SSHUser string `json:"ssh_user,omitempty"`
Bastion bool `json:"bastion,omitempty"`
}

func getMachineProvisionerOutput(instances []MachineInstance) output {
var out output

for _, instance := range instances {
machine := getMachineInfo(instance)
out.Machines = append(out.Machines, machine)
}
return out
}

func getMachineInfo(instance MachineInstance) machine {
var publicAddress, privateAddress, hostname string
for address, addressType := range instance.inst.Addresses() {
if addressType == v1.NodeExternalIP {
publicAddress = address
} else if addressType == v1.NodeInternalIP {
privateAddress = address
} else if addressType == v1.NodeHostName {
hostname = address
} else if addressType == v1.NodeInternalDNS {
hostname = address
}
}

return machine{
PublicAddress: publicAddress,
PrivateAddress: privateAddress,
Hostname: hostname,
SSHUser: instance.sshUser,
}
}

func publicAndPrivateIPExist(addresses map[string]v1.NodeAddressType) bool {
var publicIPExists, privateIPExists bool
for _, addressType := range addresses {
if addressType == v1.NodeExternalIP {
publicIPExists = true
} else if addressType == v1.NodeInternalIP {
privateIPExists = true
}
}

return publicIPExists && privateIPExists
}
Loading