Skip to content

Commit

Permalink
Merge pull request #35 from moadqassem/refactoring-osm
Browse files Browse the repository at this point in the history
Refactoring osm
  • Loading branch information
moadqassem committed Oct 14, 2021
2 parents 5b346a1 + 76bb472 commit 26ec611
Show file tree
Hide file tree
Showing 22 changed files with 1,135 additions and 310 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ linters:
- misspell
- nakedret
- scopelint
- staticcheck
- structcheck
- stylecheck
- unconvert
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,8 @@ $(CMD): %: $(BUILD_DEST)/%

$(BUILD_DEST)/%: cmd/%
go build -o $@ ./cmd/$*

.PHONY: clean
clean:
rm -rf $(BUILD_DEST)
@echo "Cleaned $(BUILD_DEST)"
2 changes: 2 additions & 0 deletions charts/crd/crd-operating-system-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ spec:
listKind: OperatingSystemConfigList
plural: operatingsystemconfigs
singular: operatingsystemconfig
shortNames:
- osc
scope: Namespaced
versions:
- name: v1alpha1
Expand Down
2 changes: 2 additions & 0 deletions charts/crd/crd-operating-system-profile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ spec:
listKind: OperatingSystemProfileList
plural: operatingsystemprofiles
singular: operatingsystemprofile
shortNames:
- osp
scope: Namespaced
versions:
- name: v1alpha1
Expand Down
63 changes: 59 additions & 4 deletions cmd/osp-controller/main.go → cmd/osm-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ package main

import (
"flag"
"fmt"
"net"
"os"
"strings"

"go.uber.org/zap"

clusterv1alpha1 "github.com/kubermatic/machine-controller/pkg/apis/cluster/v1alpha1"
"k8c.io/operating-system-manager/pkg/controllers/osc"
"k8c.io/operating-system-manager/pkg/controllers/osp"
"k8c.io/operating-system-manager/pkg/crd/osm/v1alpha1"
"k8c.io/operating-system-manager/pkg/generator"

"k8s.io/klog"
"sigs.k8s.io/controller-runtime/pkg/client/config"
Expand All @@ -34,18 +40,38 @@ import (
type options struct {
workerCount int
namespace string
clusterName string

clusterDNSIPs string
kubeconfig string
}

func main() {
klog.InitFlags(nil)

opt := &options{}

if flag.Lookup("kubeconfig") == nil {
flag.StringVar(&opt.kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
}

flag.IntVar(&opt.workerCount, "worker-count", 10, "Number of workers which process reconciliation in parallel.")
flag.StringVar(&opt.namespace, "namespace", "", "The namespace where the OSP controller will run.")
flag.StringVar(&opt.clusterName, "cluster-name", "", "The cluster where the OSC will run")
flag.StringVar(&opt.namespace, "namespace", "", "The namespace where the OSC controller will run.")

flag.StringVar(&opt.clusterDNSIPs, "cluster-dns", "10.10.10.10", "Comma-separated list of DNS server IP address.")

flag.Parse()

if len(opt.namespace) == 0 {
klog.Fatal("-namespace is required")
}
opt.kubeconfig = flag.Lookup("kubeconfig").Value.(flag.Getter).Get().(string)

clusterDNSIPs, err := parseClusterDNSIPs(opt.clusterDNSIPs)
if err != nil {
klog.Fatalf("invalid cluster dns specified: %v", err)
}

mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{})
if err != nil {
Expand All @@ -55,19 +81,48 @@ func main() {
if err = v1alpha1.AddToScheme(mgr.GetScheme()); err != nil {
klog.Fatal(err)
}

// because we watch MachineDeployments
if err = clusterv1alpha1.AddToScheme(mgr.GetScheme()); err != nil {
klog.Fatal(err)
}
logger, err := zap.NewProduction()
if err != nil {
klog.Fatal(err)
}

log := logger.Sugar()

if err := osp.Add(log, mgr, opt.namespace, opt.workerCount); err != nil {
if err := osp.Add(mgr, log, opt.namespace, opt.workerCount); err != nil {
klog.Fatal(err)
}

if err := osc.Add(
mgr,
log,
opt.namespace,
opt.clusterName,
opt.workerCount,
clusterDNSIPs,
opt.kubeconfig,
generator.NewDefaultCloudInitGenerator(""),
); err != nil {
klog.Fatal(err)
}

if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
klog.Fatalf("Failed to start OSP controller: %v", zap.Error(err))
klog.Fatalf("Failed to start OSC controller: %v", zap.Error(err))
}
}

func parseClusterDNSIPs(s string) ([]net.IP, error) {
var ips []net.IP
sips := strings.Split(s, ",")
for _, sip := range sips {
ip := net.ParseIP(strings.TrimSpace(sip))
if ip == nil {
return nil, fmt.Errorf("unable to parse ip %s", sip)
}
ips = append(ips, ip)
}
return ips, nil
}
15 changes: 11 additions & 4 deletions codegen/reconcile/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ func main() {
ImportAlias: "osmv1alpha1",
ResourceImportPath: "k8c.io/operating-system-manager/pkg/crd/osm/v1alpha1",
},
{
ResourceName: "ClusterRoleBinding",
ImportAlias: "rbacv1",
ResourceImportPath: "k8s.io/api/rbac/v1",
},
{
ResourceName: "ClusterRole",
ImportAlias: "rbacv1",
ResourceImportPath: "k8s.io/api/rbac/v1",
},
},
}

Expand All @@ -61,8 +71,6 @@ func main() {
if err := ioutil.WriteFile("zz_generated_reconcile.go", fmtB, 0644); err != nil {
log.Fatal(err)
}

fmt.Println("go go")
}

func lowercaseFirst(str string) string {
Expand All @@ -80,7 +88,6 @@ import (
"fmt"
"context"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
{{ range .Resources }}
Expand Down Expand Up @@ -157,7 +164,7 @@ type Named{{ .APIVersionPrefix }}{{ .ResourceName }}CreatorGetter = func() (name
// {{ .APIVersionPrefix }}{{ .ResourceName }}ObjectWrapper adds a wrapper so the {{ .APIVersionPrefix }}{{ .ResourceName }}Creator matches ObjectCreator.
// This is needed as Go does not support function interface matching.
func {{ .APIVersionPrefix }}{{ .ResourceName }}ObjectWrapper(create {{ .APIVersionPrefix }}{{ .ResourceName }}Creator) ObjectCreator {
return func(existing runtime.Object) (runtime.Object, error) {
return func(existing ctrlruntimeclient.Object) (ctrlruntimeclient.Object, error) {
if existing != nil {
return create(existing.(*{{ .ImportAlias }}.{{ .ResourceName }}))
}
Expand Down
63 changes: 63 additions & 0 deletions examples/osp-example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright 2021 The Kubermatic Kubernetes Platform contributors.
#
# 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.

apiVersion: operatingsystemmanager.k8c.io/v1alpha1
kind: OperatingSystemProfile
metadata:
name: test-osp
namespace: kube-system
spec:
osName: "ubuntu"
osVersion: "20.04"
supportedCloudProviders:
- name: "aws"
spec:
region: "us-west-1a"
units:
- name: test-unit-1
enable: true
content: "test unit content"
files:
- path: "/tmp/testfile-1"
permissions: 0644
content:
inline:
data: "test data"
encoding: "none"
- path: "/tmp/testfile-2"
permissions: 0644
content:
inline:
encoding: b64
data: |-
mkdir -p /opt/bin/
mkdir -p /var/lib/calico
mkdir -p /etc/kubernetes/manifests
mkdir -p /etc/cni/net.d
mkdir -p /opt/cni/bin
if [ ! -f /opt/cni/bin/loopback ]; then
curl -L https://github.com/containernetworking/plugins/releases/download/v0.8.7/cni-plugins-linux-amd64-v0.8.7.tgz | tar -xvzC /opt/cni/bin -f -
fi
{{- /* kubelet */}}
if [ ! -f /opt/bin/kubelet ]; then
curl -Lfo /opt/bin/kubelet https://storage.googleapis.com/kubernetes-release/release/v{{ .KubeletVersion }}/bin/linux/amd64/kubelet
chmod +x /opt/bin/kubelet
fi
if [[ ! -x /opt/bin/health-monitor.sh ]]; then
curl -Lfo /opt/bin/health-monitor.sh https://raw.githubusercontent.com/kubermatic/machine-controller/8b5b66e4910a6228dfaecccaa0a3b05ec4902f8e/pkg/userdata/scripts/health-monitor.sh
chmod +x /opt/bin/health-monitor.sh
fi
Loading

0 comments on commit 26ec611

Please sign in to comment.