Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cacert from rancher and use serverl-url from rancher #36

Merged
merged 6 commits into from
Jul 13, 2022
Merged
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
4 changes: 0 additions & 4 deletions chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ spec:
- {{ template "system_default_registry" . }}{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}
- --default-registry
- {{ .Values.global.cattle.systemDefaultRegistry | quote }}
- --rancher-server-url
- {{ .Values.global.cattle.url | quote }}
- --ca-cert
- {{ .Values.global.cattle.ca_cert | quote }}
serviceAccountName: elemental-operator
{{- with .Values.tolerations }}
tolerations:
Expand Down
8 changes: 8 additions & 0 deletions chart/templates/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ kind: ClusterRole
metadata:
name: elemental-operator
rules:
- apiGroups:
- management.cattle.io
resources:
- 'settings'
verbs:
- 'get'
- 'watch'
- 'list'
- apiGroups:
- ""
resources:
Expand Down
2 changes: 0 additions & 2 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ noProxy: 127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,.svc,.cluster.local
global:
cattle:
systemDefaultRegistry: ""
url: ""
ca_cert: ""

# default sync interval for upgrade channel
sync_interval: "60m"
Expand Down
22 changes: 6 additions & 16 deletions cmd/operator/operator/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ import (
)

type rootConfig struct {
Debug bool
SyncInterval time.Duration
Namespace string
DefaultRegistry string
RancherServerURL string
CACert string
OperatorImage string
SyncNamespaces []string
Debug bool
SyncInterval time.Duration
Namespace string
DefaultRegistry string
OperatorImage string
SyncNamespaces []string
}

func NewOperatorCommand() *cobra.Command {
Expand All @@ -55,9 +53,6 @@ func NewOperatorCommand() *cobra.Command {
}

viper.AutomaticEnv()
cmd.PersistentFlags().StringVar(&config.RancherServerURL, "rancher-server-url", "", "URL used to build registration url")
_ = viper.BindPFlag("rancher-server-url", cmd.PersistentFlags().Lookup("rancher-server-url"))
_ = cobra.MarkFlagRequired(cmd.PersistentFlags(), "rancher-server-url")

cmd.PersistentFlags().StringVar(&config.Namespace, "namespace", "", "namespace to run the operator on")
_ = viper.BindPFlag("namespace", cmd.PersistentFlags().Lookup("namespace"))
Expand All @@ -73,9 +68,6 @@ func NewOperatorCommand() *cobra.Command {
cmd.PersistentFlags().StringVar(&config.DefaultRegistry, "default-registry", "", "default registry to prepend to os images")
_ = viper.BindPFlag("default-registry", cmd.PersistentFlags().Lookup("default-registry"))

cmd.PersistentFlags().StringVar(&config.CACert, "ca-cert", "", "Rancher URL CA certificate")
_ = viper.BindPFlag("ca-cert", cmd.PersistentFlags().Lookup("ca-cert"))

cmd.PersistentFlags().DurationVar(&config.SyncInterval, "sync-interval", 60*time.Minute, "how often to check for new os versions")
_ = viper.BindPFlag("sync-interval", cmd.PersistentFlags().Lookup("sync-interval"))

Expand All @@ -98,8 +90,6 @@ func operatorRun(config *rootConfig) {
if err := operator.Run(ctx,
operator.WithRequeuer(requeuer),
operator.WithNamespace(config.Namespace),
operator.WithServerURL(config.RancherServerURL),
operator.WithCACert(config.CACert),
operator.WithDefaultRegistry(config.DefaultRegistry),
operator.WithServices(syncer.UpgradeChannelSync(config.SyncInterval, requeuer, config.OperatorImage, false, config.SyncNamespaces...)),
); err != nil {
Expand Down
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ require (
github.com/rancher-sandbox/go-tpm v0.0.0-20220512105546-a8efc2a6448e
github.com/rancher/fleet/pkg/apis v0.0.0-20220318160658-4dc66c946ca2
github.com/rancher/lasso v0.0.0-20210709145333-6c6cd7fd6607
github.com/rancher/rancher/pkg/apis v0.0.0-20211013185633-a636bda2a00e // indirect
github.com/rancher/steve v0.0.0-20220503004032-53511a06ff37
github.com/rancher/system-agent v0.2.8
github.com/rancher/system-upgrade-controller/pkg/apis v0.0.0-20220321230925-79da9f058209
Expand All @@ -62,9 +63,13 @@ require (
sigs.k8s.io/controller-runtime v0.11.1
)

require github.com/pin/tftp v2.1.0+incompatible // indirect

require (
github.com/pin/tftp v2.1.0+incompatible // indirect
github.com/rancher/aks-operator v1.0.2 // indirect
github.com/rancher/eks-operator v1.1.1 // indirect
github.com/rancher/gke-operator v1.1.1 // indirect
github.com/rancher/norman v0.0.0-20210608202517-59b3523c3133 // indirect
github.com/rancher/rke v1.3.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
Expand Down
162 changes: 162 additions & 0 deletions go.sum

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions pkg/clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
elmcontrollers "github.com/rancher/elemental-operator/pkg/generated/controllers/elemental.cattle.io/v1beta1"
"github.com/rancher/elemental-operator/pkg/generated/controllers/fleet.cattle.io"
fleetcontrollers "github.com/rancher/elemental-operator/pkg/generated/controllers/fleet.cattle.io/v1alpha1"
"github.com/rancher/elemental-operator/pkg/generated/controllers/management.cattle.io"
ranchercontrollers "github.com/rancher/elemental-operator/pkg/generated/controllers/management.cattle.io/v3"
"github.com/rancher/wrangler/pkg/clients"
"github.com/rancher/wrangler/pkg/generic"
"github.com/sirupsen/logrus"
Expand All @@ -45,6 +47,7 @@ type Clients struct {
Fleet fleetcontrollers.Interface
Elemental elmcontrollers.Interface
Events corev1Typed.EventInterface
Rancher ranchercontrollers.Interface
CAPI capi.Interface
}

Expand Down Expand Up @@ -79,6 +82,7 @@ func NewFromConfig(restConfig *rest.Config) (*Clients, error) {
Fleet: fleet.NewFactoryFromConfigWithOptionsOrDie(restConfig, opts).Fleet().V1alpha1(),
Elemental: elemental.NewFactoryFromConfigWithOptionsOrDie(restConfig, opts).Elemental().V1beta1(),
CAPI: capicontrollers.NewFactoryFromConfigWithOptionsOrDie(restConfig, opts).Cluster().V1beta1(),
Rancher: management.NewFactoryFromConfigWithOptionsOrDie(restConfig, opts).Management().V3(),
}, nil
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/codegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"

fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
v3 "github.com/rancher/rancher/pkg/apis/management.cattle.io/v3"
controllergen "github.com/rancher/wrangler/pkg/controller-gen"
"github.com/rancher/wrangler/pkg/controller-gen/args"
capi "sigs.k8s.io/cluster-api/api/v1beta1"
Expand Down Expand Up @@ -48,6 +49,11 @@ func main() {
capi.Machine{},
},
},
"management.cattle.io": {
Types: []interface{}{
v3.Setting{},
},
},
},
})
}
48 changes: 37 additions & 11 deletions pkg/controllers/registration/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ package registration

import (
"context"
"fmt"

elm "github.com/rancher/elemental-operator/pkg/apis/elemental.cattle.io/v1beta1"
"github.com/rancher/elemental-operator/pkg/clients"
elmcontrollers "github.com/rancher/elemental-operator/pkg/generated/controllers/elemental.cattle.io/v1beta1"
ranchercontrollers "github.com/rancher/elemental-operator/pkg/generated/controllers/management.cattle.io/v3"
"github.com/rancher/wrangler/pkg/randomtoken"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -33,18 +36,18 @@ import (
var controllerName = "machine-registration"

type handler struct {
ctx context.Context
Recorder record.EventRecorder
clients *clients.Clients
serverURL string
ctx context.Context
Recorder record.EventRecorder
clients *clients.Clients
settingCache ranchercontrollers.SettingCache
}

func Register(ctx context.Context, clients *clients.Clients, serverURL string) {
func Register(ctx context.Context, clients *clients.Clients) {
h := handler{
ctx: ctx,
clients: clients,
serverURL: serverURL,
Recorder: clients.EventRecorder(controllerName),
ctx: ctx,
clients: clients,
Recorder: clients.EventRecorder(controllerName),
settingCache: clients.Rancher.Setting().Cache(),
}
elmcontrollers.RegisterMachineRegistrationStatusHandler(ctx, clients.Elemental.MachineRegistration(), "Ready", controllerName, h.OnChange)
h.clients.Elemental.MachineRegistration().OnRemove(ctx, controllerName, h.OnRemove)
Expand All @@ -53,6 +56,11 @@ func Register(ctx context.Context, clients *clients.Clients, serverURL string) {
func (h *handler) OnChange(obj *elm.MachineRegistration, status elm.MachineRegistrationStatus) (elm.MachineRegistrationStatus, error) {
var err error

serverURL, err := h.getRancherServerURL()
if err != nil {
return status, err
}

if status.RegistrationToken == "" {
status.RegistrationToken, err = randomtoken.Generate()
if err != nil {
Expand All @@ -61,7 +69,7 @@ func (h *handler) OnChange(obj *elm.MachineRegistration, status elm.MachineRegis
}
}

status.RegistrationURL = h.serverURL + "/elemental/registration/" + status.RegistrationToken
status.RegistrationURL = fmt.Sprintf("%s/elemental/registration/%s", serverURL, status.RegistrationToken)

_, err = h.clients.RBAC.Role().Create(&rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -72,7 +80,12 @@ func (h *handler) OnChange(obj *elm.MachineRegistration, status elm.MachineRegis
APIGroups: []string{""},
Verbs: []string{"get", "watch", "list", "update", "patch"},
Resources: []string{"secrets"},
}},
}, {
APIGroups: []string{"management.cattle.io"},
Verbs: []string{"get", "watch", "list"},
Resources: []string{"settings"},
},
},
})
if err != nil && !apierrors.IsAlreadyExists(err) {
return status, err
Expand Down Expand Up @@ -133,3 +146,16 @@ func (h *handler) OnRemove(_ string, obj *elm.MachineRegistration) (*elm.Machine

return nil, nil
}

func (h *handler) getRancherServerURL() (string, error) {
setting, err := h.settingCache.Get("server-url")
if err != nil {
logrus.Errorf("Error getting server-url setting: %s", err.Error())
return "", err
}
if setting.Value == "" {
logrus.Error("server-url is not set")
return "", fmt.Errorf("server-url is not set")
}
return setting.Value, nil
}
67 changes: 67 additions & 0 deletions pkg/generated/controllers/management.cattle.io/factory.go

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

43 changes: 43 additions & 0 deletions pkg/generated/controllers/management.cattle.io/interface.go

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

Loading