Skip to content

Commit

Permalink
Merge elemental installer (#20)
Browse files Browse the repository at this point in the history
* Move main into a cmd/operator package
* Add elemental-installer
* Adding installer unit tests
* Update Makefile
* Update .github/workflows/unit-tests.yaml
* Adapat Dockerfile and golreleaser to keep releasing and building elemental-operator as they used to

Signed-off-by: David Cassany <[email protected]>
Co-authored-by: Itxaka <[email protected]>
  • Loading branch information
davidcassany and Itxaka authored Jul 7, 2022
1 parent 211ad46 commit 1d97f14
Show file tree
Hide file tree
Showing 25 changed files with 2,064 additions and 14 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/unit-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Unit tests
on:
push:
concurrency:
group: tests-${{ github.head_ref || github.ref }}-${{ github.repository }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: '^1.16'
- name: Get dependencies
run: |
# Needed for github.com/google/go-tspi/tspi
# in opensuse this is trousers-devel
sudo apt-get install libtspi-dev
make unit-tests-deps
- name: Run tests
run: |
make unit-tests
- name: Codecov
uses: codecov/codecov-action@v2
with:
file: ./coverage.out
5 changes: 3 additions & 2 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
project_name: rancheros-operator
builds:
- env:
- main: ./cmd/operator
env:
- CGO_ENABLED=0
ldflags:
- -extldflags -static -s
Expand Down Expand Up @@ -34,4 +35,4 @@ changelog:
exclude:
- '^docs:'
- '^test:'
- '^Merge pull request'
- '^Merge pull request'
18 changes: 16 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ CHART?=$(shell find $(ROOT_DIR) -type f -name "elemental-operator*.tgz" -print)
CHART_VERSION?=$(subst v,,$(GIT_TAG))

.PHONY: build
build:
CGO_ENABLED=0 go build -ldflags "-extldflags -static -s -X 'github.com/rancher/elemental-operator/version.Version=$TAG'" -o build/elemental-operator
build: operator installer

.PHONY: operator
operator:
CGO_ENABLED=0 go build -ldflags "-extldflags -static -s -X 'github.com/rancher/elemental-operator/version.Version=$TAG'" -o build/elemental-operator $(ROOT_DIR)/cmd/operator

.PHONY: installer
installer:
CGO_ENABLED=0 go build -ldflags "-extldflags -static -s -X 'github.com/rancher/elemental-operator/version.Version=$TAG'" -o build/elemental-installer $(ROOT_DIR)/cmd/installer

.PHONY: build-docker
build-docker:
Expand All @@ -29,3 +36,10 @@ chart:

validate:
scripts/validate

unit-tests-deps:
go install -mod=mod github.com/onsi/ginkgo/v2/ginkgo@latest
go get github.com/onsi/gomega/...

unit-tests:
ginkgo -r -v --covermode=atomic --coverprofile=coverage.out ./pkg/...
59 changes: 59 additions & 0 deletions cmd/installer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright © 2022 SUSE LLC
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"
"flag"
"os"

"github.com/rancher/elemental-operator/pkg/config"
"github.com/rancher/elemental-operator/pkg/install"
"github.com/sirupsen/logrus"
"sigs.k8s.io/yaml"
)

var (
automatic = flag.Bool("automatic", false, "Check for and run automatic installation")
printConfig = flag.Bool("print-config", false, "Print effective configuration and exit")
configFile = flag.String("config-file", "/oem/userdata", "Config file to use, local file or http/tftp URL")
powerOff = flag.Bool("power-off", false, "Power off after installation")
reboot = flag.Bool("reboot", false, "Reboot after installation")
noRebootAutomatic = flag.Bool("no-reboot-automatic", false, "Dont reboot after installation (only for automatic installation which defaults to reboot after install)")
yes = flag.Bool("y", false, "Do not prompt for questions")
ejectCD = flag.Bool("eject-cd", false, "Ejects the CD on system reboot")
)

func main() {
flag.Parse()
if *printConfig {
cfg, err := config.ReadConfig(context.Background(), *configFile, *automatic)
if err != nil {
logrus.Fatal(err)
}
data, err := yaml.Marshal(cfg)
if err != nil {
logrus.Fatal(err)
}
os.Stdout.Write(data)
return
}

if err := install.Run(*automatic, *configFile, *powerOff, *reboot, *noRebootAutomatic, *yes, *ejectCD); err != nil {
logrus.Fatal(err)
}
}
File renamed without changes.
6 changes: 3 additions & 3 deletions main.go → cmd/operator/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
display_cmd "github.com/rancher/elemental-operator/cmd/display"
operator_cmd "github.com/rancher/elemental-operator/cmd/operator"
register_cmd "github.com/rancher/elemental-operator/cmd/register"
display_cmd "github.com/rancher/elemental-operator/cmd/operator/display"
operator_cmd "github.com/rancher/elemental-operator/cmd/operator/operator"
register_cmd "github.com/rancher/elemental-operator/cmd/operator/register"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.12.2 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-isatty v0.0.14
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/mitchellh/mapstructure v1.4.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand All @@ -125,6 +126,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/tredoe/osutil v1.0.6
github.com/twpayne/go-vfs v1.5.0 // indirect
github.com/urfave/cli v1.22.7 // indirect
go.opentelemetry.io/contrib v1.6.0 // indirect
Expand All @@ -145,7 +147,7 @@ require (
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220411224347-583f2d630306 // indirect
golang.org/x/tools v0.1.10 // indirect
Expand Down Expand Up @@ -173,5 +175,5 @@ require (
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.22 // indirect
sigs.k8s.io/cli-utils v0.16.0 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.0 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
sigs.k8s.io/yaml v1.3.0
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,7 @@ github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2y
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
Expand Down Expand Up @@ -1404,6 +1405,10 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20200427203606-3cfed13b9966/go.mod h1
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce/go.mod h1:o8v6yHRoik09Xen7gje4m9ERNah1d1PPsVq1VEx9vE4=
github.com/transparency-dev/merkle v0.0.1/go.mod h1:B8FIw5LTq6DaULoHsVFRzYIUDkl8yuSwCdZnOZGKL/A=
github.com/tredoe/fileutil v1.0.5/go.mod h1:HFzzpvg+3Q8LgmZgo1mVF5epHc/CVkWKEb3hja+/1Zo=
github.com/tredoe/goutil v1.0.0/go.mod h1:Qhf75QLcNEChimbl4wb8nROzw9PCFCPYTEUmTnoszXY=
github.com/tredoe/osutil v1.0.6 h1:KJvG9AFmUPLe3hsNKyPMIjNx77CkAJtMKVS4ugAT7vM=
github.com/tredoe/osutil v1.0.6/go.mod h1:zNq93p2DLHJWkHi2/+zi3xOjZl8xxiv3tiI2A6zcB3w=
github.com/tredoe/osutil/v2 v2.0.0-rc.16/go.mod h1:uLRVx/3pb7Y4RQhG8cQFbPE9ha5r81e6MXpBsxbTAYc=
github.com/twpayne/go-vfs v1.5.0 h1:3j9j2RchDSIQ1gjuNHZqJOyZAS702lrmqUow2uUYgX8=
github.com/twpayne/go-vfs v1.5.0/go.mod h1:MAbvpnWRAE5LfIt88Eopskigmyc+wakYCyCDnys7iNw=
Expand Down
5 changes: 2 additions & 3 deletions package/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ FROM golang:1.17-alpine as build
WORKDIR /src
COPY go.mod go.sum /src/
RUN go mod download
COPY main.go /src/
COPY cmd /src/cmd
COPY pkg /src/pkg
RUN CGO_ENABLED=0 go build -ldflags "-extldflags -static -s" -o /usr/bin/elemental-operator
RUN CGO_ENABLED=0 go build -ldflags "-extldflags -static -s" -o /usr/bin/elemental-operator ./cmd/operator

FROM registry.suse.com/suse/sle15:15.3 as elemental-operator
COPY --from=build /usr/bin/elemental-operator /usr/bin/elemental-operator

ENTRYPOINT ["elemental-operator"]
ENTRYPOINT ["elemental-operator"]
102 changes: 102 additions & 0 deletions pkg/config/coerce.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
Copyright © 2022 SUSE LLC
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 config

import (
"github.com/rancher/wrangler/pkg/data"
"github.com/rancher/wrangler/pkg/data/convert"
schemas2 "github.com/rancher/wrangler/pkg/schemas"
"github.com/rancher/wrangler/pkg/schemas/mappers"
)

type Converter func(val interface{}) interface{}

type fieldConverter struct {
mappers.DefaultMapper
fieldName string
converter Converter
}

func (f fieldConverter) ToInternal(data data.Object) error {
val, ok := data[f.fieldName]
if !ok {
return nil
}
data[f.fieldName] = f.converter(val)
return nil
}

type typeConverter struct {
mappers.DefaultMapper
converter Converter
fieldType string
mappers schemas2.Mappers
}

func (t *typeConverter) ToInternal(data data.Object) error {
return t.mappers.ToInternal(data)
}

func (t *typeConverter) ModifySchema(schema *schemas2.Schema, schemas *schemas2.Schemas) error {
for name, field := range schema.ResourceFields {
if field.Type == t.fieldType {
t.mappers = append(t.mappers, fieldConverter{
fieldName: name,
converter: t.converter,
})
}
}
return nil
}

func NewTypeConverter(fieldType string, converter Converter) schemas2.Mapper {
return &typeConverter{
fieldType: fieldType,
converter: converter,
}
}

func NewToMap() schemas2.Mapper {
return NewTypeConverter("map[string]", func(val interface{}) interface{} {
if m, ok := val.(map[string]interface{}); ok {
obj := make(map[string]string, len(m))
for k, v := range m {
obj[k] = convert.ToString(v)
}
return obj
}
return val
})
}

func NewToSlice() schemas2.Mapper {
return NewTypeConverter("array[string]", func(val interface{}) interface{} {
if str, ok := val.(string); ok {
return []string{str}
}
return val
})
}

func NewToBool() schemas2.Mapper {
return NewTypeConverter("boolean", func(val interface{}) interface{} {
if str, ok := val.(string); ok {
return str == "true" //nolint:goconst
}
return val
})
}
69 changes: 69 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright © 2022 SUSE LLC
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 config

type RancherOS struct {
Install Install `json:"install,omitempty"`
}

type Install struct {
Automatic bool `json:"automatic,omitempty"`
Firmware string `json:"firmware,omitempty"`
Device string `json:"device,omitempty"`
ConfigURL string `json:"configUrl,omitempty"`
ISOURL string `json:"isoUrl,omitempty"`
ContainerImage string `json:"containerImage,omitempty"`
PowerOff bool `json:"powerOff,omitempty"`
Reboot bool `json:"reboot,omitempty"`
NoFormat bool `json:"noFormat,omitempty"`
Debug bool `json:"debug,omitempty"`
TTY string `json:"tty,omitempty"`
ServerURL string `json:"-"`
Token string `json:"-"`
Role string `json:"-"`
Password string `json:"password,omitempty"`
RegistrationURL string `json:"registrationUrl,omitempty"`
RegistrationCACert string `json:"registrationCaCert,omitempty"`
EjectCD bool `json:"ejectCD,omitempty"`
}

type Config struct {
SSHAuthorizedKeys []string `json:"ssh_authorized_keys,omitempty"`
RancherOS RancherOS `json:"rancheros,omitempty"`
Data map[string]interface{} `json:"-"`
}

type YipConfig struct {
Stages map[string][]Stage `json:"stages,omitempty"`
Rancherd Rancherd `json:"rancherd,omitempty"`
}

type Stage struct {
Users map[string]User `json:"users,omitempty"`
}

type Rancherd struct {
Server string `json:"server,omitempty"`
Role string `json:"role,omitempty"`
Token string `json:"token,omitempty"`
}

type User struct {
Name string `json:"name,omitempty"`
PasswordHash string `json:"passwd,omitempty"`
SSHAuthorizedKeys []string `json:"ssh_authorized_keys,omitempty"`
}
Loading

0 comments on commit 1d97f14

Please sign in to comment.