Skip to content

Commit

Permalink
Support ProviderOptions from the Kairos SDK; export constants for yip…
Browse files Browse the repository at this point in the history
… stage names (#65)

* conditionally disable ClusterInit to use sqlite for two node clusters
* expose boot.before stage names as constants
* support kairos SDK ProviderOptions; add datastore config
---------

Signed-off-by: Tyler Gillson <[email protected]>
Signed-off-by: Arun Sharma <[email protected]>
Signed-off-by: Oz Tiram <[email protected]>
Co-authored-by: Arun Sharma <[email protected]>
Co-authored-by: Oz Tiram <[email protected]>
  • Loading branch information
3 people authored Jun 11, 2024
1 parent bd434c0 commit e0c5ebe
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 61 deletions.
3 changes: 3 additions & 0 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ BUILD_GOLANG:
COPY . ./
ARG BIN
ARG SRC
ENV CGO_ENABLED=0
RUN go-build-static.sh -a -o ${BIN} ./${SRC}
SAVE ARTIFACT ${BIN} ${BIN} AS LOCAL build/${BIN}

Expand All @@ -60,13 +61,15 @@ lint:
build-provider:
FROM +go-deps
DO +BUILD_GOLANG --BIN=agent-provider-k3s --SRC=main.go

build-provider-package:
DO +VERSION
ARG VERSION=$(cat VERSION)
FROM scratch
COPY +build-provider/agent-provider-k3s /system/providers/agent-provider-k3s
COPY scripts /opt/k3s/scripts
SAVE IMAGE --push $IMAGE_REPOSITORY/provider-k3s:${VERSION}

docker:
DO +VERSION
ARG VERSION=$(cat VERSION)
Expand Down
15 changes: 10 additions & 5 deletions api/k3s_config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package api

type K3sServerConfig struct {
ClusterInit bool `yaml:"cluster-init,omitempty"`
Token string `yaml:"token,omitempty"`
Server string `yaml:"server,omitempty"`
TLSSan []string `yaml:"tls-san,omitempty"`
DatastoreEndpoint string `yaml:"datastore-endpoint,omitempty"`
// ClusterInit must not have an omitempty tag, otherwise it is elided by JSON/YAML encoders when false.
ClusterInit bool `yaml:"cluster-init" json:"cluster-init"`
Token string `yaml:"token,omitempty" json:"token,omitempty"`
Server string `yaml:"server,omitempty" json:"server,omitempty"`
TLSSan []string `yaml:"tls-san,omitempty" json:"tls-san,omitempty"`
DatastoreEndpoint string `yaml:"datastore-endpoint,omitempty" json:"datastore-endpoint,omitempty"`
DatastoreCaFile string `yaml:"datastore-cafile,omitempty" json:"datastore-cafile,omitempty"`
DatastoreCertFile string `yaml:"datastore-certfile,omitempty" json:"datastore-certfile,omitempty"`
DatastoreKeyFile string `yaml:"datastore-keyfile,omitempty" json:"datastore-keyfile,omitempty"`
BindAddress string `yaml:"bind-address,omitempty" json:"bind-address,omitempty"`
}
17 changes: 7 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,29 @@ module github.com/kairos-io/provider-k3s
go 1.18

require (
github.com/kairos-io/kairos-sdk v0.0.2-0.20230414094028-0c9d2bd9e6ae
github.com/mudler/yip v1.0.0
github.com/sirupsen/logrus v1.9.0
gopkg.in/yaml.v2 v2.4.0
github.com/kairos-io/kairos-sdk v0.0.16-0.20231030162246-196f133f2667
github.com/mudler/yip v1.3.0
github.com/sirupsen/logrus v1.9.3
gopkg.in/yaml.v3 v3.0.1
sigs.k8s.io/yaml v1.3.0
)

require (
github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/google/pprof v0.0.0-20230228050547-1710fef4ab10 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/itchyny/gojq v0.12.12 // indirect
github.com/itchyny/gojq v0.12.13 // indirect
github.com/itchyny/timefmt-go v0.1.5 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/mudler/go-pluggable v0.0.0-20230126220627-7710299a0ae5 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/onsi/ginkgo/v2 v2.9.2 // indirect
github.com/onsi/gomega v1.27.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/twpayne/go-vfs v1.7.2 // indirect
github.com/twpayne/go-vfs/v4 v4.2.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/sys v0.10.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
39 changes: 18 additions & 21 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
Expand All @@ -22,7 +22,6 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/pprof v0.0.0-20230228050547-1710fef4ab10 h1:CqYfpuYIjnlNxM3msdyPRKabhXZWbKjf3Q8BWROFBso=
github.com/google/pprof v0.0.0-20230228050547-1710fef4ab10/go.mod h1:79YE0hCXdHag9sBkw2o+N/YnZtTkXi0UT9Nnixa5eYk=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand All @@ -31,12 +30,12 @@ github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/itchyny/gojq v0.12.12 h1:x+xGI9BXqKoJQZkr95ibpe3cdrTbY8D9lonrK433rcA=
github.com/itchyny/gojq v0.12.12/go.mod h1:j+3sVkjxwd7A7Z5jrbKibgOLn0ZfLWkV+Awxr/pyzJE=
github.com/itchyny/gojq v0.12.13 h1:IxyYlHYIlspQHHTE0f3cJF0NKDMfajxViuhBLnHd/QU=
github.com/itchyny/gojq v0.12.13/go.mod h1:JzwzAqenfhrPUuwbmEz3nu3JQmFLlQTQMUcOdnu/Sf4=
github.com/itchyny/timefmt-go v0.1.5 h1:G0INE2la8S6ru/ZI5JecgyzbbJNs5lG1RcBqa7Jm6GE=
github.com/itchyny/timefmt-go v0.1.5/go.mod h1:nEP7L+2YmAbT2kZ2HfSs1d8Xtw9LY8D2stDBckWakZ8=
github.com/kairos-io/kairos-sdk v0.0.2-0.20230414094028-0c9d2bd9e6ae h1:u/1QiU5IAJNDPxsBWBQFKQxLJKcDog1aMrN0unaP18w=
github.com/kairos-io/kairos-sdk v0.0.2-0.20230414094028-0c9d2bd9e6ae/go.mod h1:Wg/jfAQe8seka5VUXtcPvg+sA6GmQEy+DYlJmgKM8Zs=
github.com/kairos-io/kairos-sdk v0.0.16-0.20231030162246-196f133f2667 h1:dASw5xel1gMlQpwQaiK4Ost3HEPx2gHsn/MEhso9Lgo=
github.com/kairos-io/kairos-sdk v0.0.16-0.20231030162246-196f133f2667/go.mod h1:Ew3NKFuXByu3Y3yGu8Q92M3oMqsXrg2VilouubdhYqA=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
Expand All @@ -47,34 +46,32 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mudler/go-pluggable v0.0.0-20230126220627-7710299a0ae5 h1:FaZD86+A9mVt7lh9glAryzQblMsbJYU2VnrdZ8yHlTs=
github.com/mudler/go-pluggable v0.0.0-20230126220627-7710299a0ae5/go.mod h1:WmKcT8ONmhDQIqQ+HxU+tkGWjzBEyY/KFO8LTGCu4AI=
github.com/mudler/yip v1.0.0 h1:GyW+XdkpS0PaAJh4rARiP8Bn/PmT+OOk190UJasO/z4=
github.com/mudler/yip v1.0.0/go.mod h1:YJBE9DrPfBnNG+BmTy3zpI8dzZ2JJCEbZkrmvuHuNGQ=
github.com/mudler/yip v1.3.0 h1:MjVh4dDr/imwJ46qXGbftnLRKmDgzs0Y60WyVtXY4i4=
github.com/mudler/yip v1.3.0/go.mod h1:3WeDh6tGX1yYPJom05E7xEjw8dNVlkH2WFxLi7Gflzk=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.14.2/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo/v2 v2.9.2 h1:BA2GMJOtfGAfagzYtrAlufIP0lq6QERkFmHLMLPwFSU=
github.com/onsi/ginkgo/v2 v2.9.2/go.mod h1:WHcJJG2dIlcCqVfBAwUCrJxSPFb6v4azBwgxeMeDuts=
github.com/onsi/ginkgo/v2 v2.11.0 h1:WgqUCUt/lT6yXoQ8Wef0fsNn5cAuMK7+KT9UFRz2tcU=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.10.3/go.mod h1:V9xEwhxec5O8UDM77eCW8vLymOMltsqPVYWrpDsH8xc=
github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
github.com/onsi/gomega v1.27.6/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg=
github.com/onsi/gomega v1.27.8 h1:gegWiwZjBsf2DgiSbf5hpokZ98JVDMcWkUiigk6/KXc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/twpayne/go-vfs v1.7.2 h1:ZNYMAXcu2Av8c109USrSGYm8dIIIV0xPlG19I2088Kw=
github.com/twpayne/go-vfs v1.7.2/go.mod h1:1eni2ntkiiAHZG27xfLOO4CYvMR4Kw8V7rYiLeeolsQ=
github.com/twpayne/go-vfs/v4 v4.2.0 h1:cIjUwaKSCq0y6dT+ev6uLSmKjGTbHCR4xaocROqHFsE=
Expand All @@ -85,8 +82,7 @@ golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.13.0 h1:Nvo8UFsZ8X3BhAC9699Z1j7XQ3rsZnUUm7jfBEk1ueY=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -101,14 +97,14 @@ golang.org/x/sys v0.0.0-20201223074533-0d417f636930/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=
golang.org/x/tools v0.9.3 h1:Gn1I8+64MsuTb/HpH+LmQtNas23LhUVr3rYZ0eKuaMM=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
Expand All @@ -133,5 +129,6 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
58 changes: 33 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"net"
Expand All @@ -10,48 +9,48 @@ import (
"strings"

"github.com/kairos-io/kairos-sdk/clusterplugin"
"github.com/kairos-io/provider-k3s/api"

yip "github.com/mudler/yip/pkg/schema"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
kyaml "sigs.k8s.io/yaml"

"github.com/kairos-io/provider-k3s/api"
"github.com/kairos-io/provider-k3s/pkg/constants"
)

const (
configurationPath = "/etc/rancher/k3s/config.d"
containerdEnvConfigPath = "/etc/default"
localImagesPath = "/opt/content/images"

serverSystemName = "k3s"
agentSystemName = "k3s-agent"
K8sNoProxy = ".svc,.svc.cluster,.svc.cluster.local"
BootBefore = "boot.before"
LocalImagesPath = "/opt/content/images"

bootBefore = "boot.before"
k8sNoProxy = ".svc,.svc.cluster,.svc.cluster.local"
)

func clusterProvider(cluster clusterplugin.Cluster) yip.YipConfig {
k3sConfig := api.K3sServerConfig{
k3sConfig := &api.K3sServerConfig{
Token: cluster.ClusterToken,
}
userOptionConfig := cluster.Options

logrus.Infof("current node role %s", cluster.Role)
logrus.Infof("received cluster options %s", cluster.Options)

var userOptionConfig string
switch cluster.Role {
case clusterplugin.RoleInit:
k3sConfig.ClusterInit = true
k3sConfig.TLSSan = []string{cluster.ControlPlaneHost}
userOptionConfig = cluster.Options
case clusterplugin.RoleControlPlane:
k3sConfig.Server = fmt.Sprintf("https://%s:6443", cluster.ControlPlaneHost)
k3sConfig.TLSSan = []string{cluster.ControlPlaneHost}
userOptionConfig = cluster.Options
case clusterplugin.RoleWorker:
userOptionConfig = ""
k3sConfig.Server = fmt.Sprintf("https://%s:6443", cluster.ControlPlaneHost)

//Data received from upstream contains config for both control plane and worker. Thus, for worker, config is being filtered
//via unmarshal into agent config.
// Data received from upstream contains config for both control plane and worker. Thus, for worker,
// config is being filtered via unmarshal into agent config.
var agentCfg api.K3sAgentConfig
if err := yaml.Unmarshal([]byte(cluster.Options), &agentCfg); err == nil {
out, _ := yaml.Marshal(agentCfg)
Expand All @@ -61,17 +60,26 @@ func clusterProvider(cluster clusterplugin.Cluster) yip.YipConfig {
}
}

// if provided, parse additional K3s server options (which may override the above settings)
if cluster.ProviderOptions != nil {
providerOpts, err := yaml.Marshal(cluster.ProviderOptions)
if err != nil {
logrus.Fatalf("failed to marshal cluster.ProviderOptions: %v", err)
}
if err := yaml.Unmarshal(providerOpts, k3sConfig); err != nil {
logrus.Fatalf("failed to unmarshal cluster.ProviderOptions: %v", err)
}
logrus.Infof("applied cluster provider options: %+v", cluster.ProviderOptions)
}

systemName := serverSystemName
if cluster.Role == clusterplugin.RoleWorker {
systemName = agentSystemName
}

var providerConfig bytes.Buffer
_ = yaml.NewEncoder(&providerConfig).Encode(&k3sConfig)

userOptions, _ := kyaml.YAMLToJSON([]byte(userOptionConfig))
proxyOptions, _ := kyaml.YAMLToJSON([]byte(cluster.Options))
options, _ := kyaml.YAMLToJSON(providerConfig.Bytes())
options, _ := json.Marshal(k3sConfig)

logrus.Infof("received cluster env %+v", cluster.Env)

Expand Down Expand Up @@ -102,7 +110,7 @@ func clusterProvider(cluster clusterplugin.Cluster) yip.YipConfig {
var stages []yip.Stage

stages = append(stages, yip.Stage{
Name: "Install K3s Configuration Files",
Name: constants.InstallK3sConfigFiles,
Files: files,
Commands: []string{
fmt.Sprintf("jq -s 'def flatten: reduce .[] as $i([]; if $i | type == \"array\" then . + ($i | flatten) else . + [$i] end); [.[] | to_entries] | flatten | reduce .[] as $dot ({}; .[$dot.key] += $dot.value)' %s/*.yaml > /etc/rancher/k3s/config.yaml", configurationPath),
Expand All @@ -112,11 +120,11 @@ func clusterProvider(cluster clusterplugin.Cluster) yip.YipConfig {
var importStage yip.Stage
if cluster.ImportLocalImages {
if cluster.LocalImagesPath == "" {
cluster.LocalImagesPath = LocalImagesPath
cluster.LocalImagesPath = localImagesPath
}

importStage = yip.Stage{
Name: "Run K3s Import Images Script",
Name: constants.ImportK3sImages,
Commands: []string{
"chmod +x /opt/k3s/scripts/import.sh",
fmt.Sprintf("/bin/sh /opt/k3s/scripts/import.sh %s > /var/log/k3s-import-images.log", cluster.LocalImagesPath),
Expand All @@ -127,15 +135,15 @@ func clusterProvider(cluster clusterplugin.Cluster) yip.YipConfig {

stages = append(stages,
yip.Stage{
Name: "Enable OpenRC Services",
Name: constants.EnableOpenRCServices,
If: "[ -x /sbin/openrc-run ]",
Commands: []string{
fmt.Sprintf("rc-update add %s default >/dev/null", systemName),
fmt.Sprintf("service %s start", systemName),
},
},
yip.Stage{
Name: "Enable Systemd Services",
Name: constants.EnableSystemdServices,
If: "[ -x /bin/systemctl ]",
Commands: []string{
fmt.Sprintf("systemctl enable %s", systemName),
Expand All @@ -147,7 +155,7 @@ func clusterProvider(cluster clusterplugin.Cluster) yip.YipConfig {
cfg := yip.YipConfig{
Name: "K3s Kairos Cluster Provider",
Stages: map[string][]yip.Stage{
BootBefore: stages,
bootBefore: stages,
},
}

Expand Down Expand Up @@ -214,7 +222,7 @@ func getDefaultNoProxy(proxyOptions []byte) string {
noProxy = noProxy + "," + serviceCIDR
}
}
noProxy = noProxy + "," + getNodeCIDR() + "," + K8sNoProxy
noProxy = noProxy + "," + getNodeCIDR() + "," + k8sNoProxy

return noProxy
}
Expand Down
Loading

0 comments on commit e0c5ebe

Please sign in to comment.