Skip to content

Commit

Permalink
Merge pull request #286 from knabben/knabben/kubernetes
Browse files Browse the repository at this point in the history
Kubernetes command for copy binaries for services
  • Loading branch information
knabben authored Jan 17, 2024
2 parents d8c7977 + 5b972f4 commit 552b42c
Show file tree
Hide file tree
Showing 22 changed files with 582 additions and 237 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: golangci-lint
on:
push:
tags:
- v*
branches:
- main
pull_request:
permissions:
contents: read
pull-requests: read
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.21
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
working-directory: experiments/swdt/
version: latest
args: -v
26 changes: 26 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: unit-tests
on:
push:
tags:
- v*
branches:
- main
pull_request:
permissions:
contents: read
pull-requests: read
jobs:
unit-test:
name: unit-test
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.21
- uses: actions/checkout@v3
- name: Run unit tests
run: |
pushd experiments/swdt
make test
make build
popd
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ sync/shared/config
sync/shared/kubejoin.ps1
sync/shared/kubeadm.yaml
sync/shared/variables.yaml
kubernetes
.vagrant/

# semaphores during build
Expand Down
17 changes: 5 additions & 12 deletions experiments/swdt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,6 @@ CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen

##@ General

# The help target prints out all targets with their descriptions organized
# beneath their categories. The categories are represented by '##@' and the
# target descriptions by '##'. The awk commands is responsible for reading the
# entire set of makefiles included in this invocation, looking for lines of the
# file as xyz: ## something, and then pretty-format the target and help. Then,
# if there's a line with ##@ something, that gets pretty-printed as a category.
# More info on the usage of ANSI control characters for terminal formatting:
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
# More info on the awk command:
# http://linuxcommand.org/lc3_adv_awk.php

.PHONY: help
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
Expand All @@ -60,4 +49,8 @@ generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and

.PHONY: test
test:
go test ./... -v 2
go test ./... -v 2

.PHONY: build
build:
go build -o swdt .
3 changes: 3 additions & 0 deletions experiments/swdt/apis/config/v1alpha1/config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type CredentialsSpec struct {
// Hostname set the Windows node endpoint
Hostname string `json:"hostname,omitempty"`

// Password is the SSH password for this user
Password string `json:"password,omitempty"`

// PrivateKey is the SSH private path for this user
PrivateKey string `json:"privateKey,omitempty"`
}
Expand Down
72 changes: 36 additions & 36 deletions experiments/swdt/apis/config/v1alpha1/zz_generated.deepcopy.go

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

54 changes: 54 additions & 0 deletions experiments/swdt/cmd/kubernetes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2024 The Kubernetes 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 cmd

import (
"github.com/spf13/cobra"
"swdt/apis/config/v1alpha1"
"swdt/pkg/pwsh/executor"
"swdt/pkg/pwsh/kubernetes"
)

func init() {
rootCmd.AddCommand(kubernetesCmd)
}

// setupCmd represents the setup command
var kubernetesCmd = &cobra.Command{
Use: "kubernetes",
Short: "Provision Kubernetes binaries into a running node",
Long: `Provision Kubernetes binaries into a running node`,
RunE: RunKubernetes,
}

func RunKubernetes(cmd *cobra.Command, args []string) error {
var (
err error
nodeConfig *v1alpha1.Node
)
if nodeConfig, err = loadConfiguration(); err != nil {
return err
}

runner, err := executor.NewRunner(nodeConfig, &kubernetes.KubernetesRunner{})
if err != nil {
return err
}
defer runner.CloseConnection() //nolint

return runner.Inner.InstallProvisioners(nodeConfig.Spec.Kubernetes.Provisioners)
}
11 changes: 11 additions & 0 deletions experiments/swdt/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ package cmd
import (
"github.com/spf13/cobra"
"os"
"swdt/apis/config/v1alpha1"
"swdt/pkg/config"
)

func init() {
rootCmd.PersistentFlags().StringP("config", "c", "samples/config.yaml", "Configuration file path.")
}

// rootCmd represents the base command
var rootCmd = &cobra.Command{
Use: "swdt",
Expand All @@ -29,6 +35,11 @@ var rootCmd = &cobra.Command{
Check the subcommands.`,
}

// loadConfiguration marshal the YAML configuration in an internal struct
func loadConfiguration() (*v1alpha1.Node, error) {
return config.LoadConfigNodeFromFile(rootCmd.Flag("config").Value.String())
}

func Execute() {
err := rootCmd.Execute()
if err != nil {
Expand Down
46 changes: 22 additions & 24 deletions experiments/swdt/cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,50 @@ limitations under the License.
package cmd

import (
"swdt/pkg/config"
"swdt/pkg/connections"
"swdt/apis/config/v1alpha1"
"swdt/pkg/pwsh/executor"
"swdt/pkg/pwsh/setup"

"github.com/spf13/cobra"
)

var configFile, password string

func init() {
rootCmd.AddCommand(setupCmd)
setupCmd.Flags().StringVarP(&configFile, "config", "c", "samples/config.yaml", "Configuration file path.")
setupCmd.Flags().StringVarP(&password, "password", "p", "", "SSH Password -- should read from stdin.")
}

// setupCmd represents the setup command
var setupCmd = &cobra.Command{
Use: "setup",
Short: "Bootstrap the node via basic unit setup.",
Long: `Bootstrap the node via basic unit setup.`,
RunE: Run,
Short: "Bootstrap the node via basic unit setup",
Long: `Bootstrap the node via basic unit setup`,
RunE: RunSetup,
}

func Run(cmd *cobra.Command, args []string) error {
// TODO(mloskot): For now load Windows node config only, but will this become config.LoadConfigFromFile for whole cluster config is implemented?
configuration, err := config.LoadConfigNodeFromFile(configFile)
if err != nil {
func RunSetup(cmd *cobra.Command, args []string) error {
var (
err error
nodeConfig *v1alpha1.Node
)
if nodeConfig, err = loadConfiguration(); err != nil {
return err
}

sshConnection := connections.NewConnection(password, configuration.Spec.Cred)
defer sshConnection.Close()
if err := sshConnection.Connect(); err != nil {
runner, err := executor.NewRunner(nodeConfig, &setup.SetupRunner{})
if err != nil {
return err
}
defer runner.CloseConnection() //nolint

return runSteps(configuration.Spec.Setup.ChocoPackages, sshConnection)
}

func runSteps(packages *[]string, conn connections.Connection) error {
var err error
// Install choco binary
if err = runner.Inner.InstallChoco(); err != nil {
return err
}

runner := setup.Runner{Run: conn.Run, Copy: conn.Copy}
if err = runner.InstallChoco(); err != nil {
// Install Choco packages from the input list
if err = runner.Inner.InstallChocoPackages(*nodeConfig.Spec.Setup.ChocoPackages); err != nil {
return err
}

return runner.InstallChocoPackages(*packages)
// Enable RDP if the option is true
return runner.Inner.EnableRDP(*nodeConfig.Spec.Setup.EnableRDP)
}
3 changes: 2 additions & 1 deletion experiments/swdt/pkg/config/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func init() {
utilruntime.Must(v1alpha1.AddToScheme(scheme))
}

// LoadConfigFromFile returns the marshalled Node configuration object
// LoadConfigNodeFromFile LoadConfigFromFile returns the marshalled Node configuration object
func LoadConfigNodeFromFile(file string) (*v1alpha1.Node, error) {
data, err := os.ReadFile(file)
if err != nil {
Expand All @@ -56,5 +56,6 @@ func loadConfigNode(data []byte) (*v1alpha1.Node, error) {
if !ok {
return nil, fmt.Errorf("got unexpected config type: %v", gvk)
}
config.Spec.Defaults()
return config, nil
}
1 change: 0 additions & 1 deletion experiments/swdt/pkg/config/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ spec:`

func TestLoadConfigNodeDefaults(t *testing.T) {
config, err := loadConfigNode([]byte(SAMPLE_DEFAULT))
config.Spec.Defaults()
assert.Nil(t, err)

assert.True(t, *config.Spec.Setup.EnableRDP)
Expand Down
4 changes: 2 additions & 2 deletions experiments/swdt/pkg/connections/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ type Connection interface {
Close() error
}

func NewConnection(password string, credentials v1alpha1.CredentialsSpec) Connection {
return &SSHConnection{credentials: &credentials, password: password}
func NewConnection(credentials v1alpha1.CredentialsSpec) Connection {
return &SSHConnection{credentials: &credentials}
}
Loading

0 comments on commit 552b42c

Please sign in to comment.