diff --git a/Dockerfile b/Dockerfile index 21b565f..677de85 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Build the manager binary -FROM golang:1.17 as builder +FROM golang:1.18 as builder WORKDIR /workspace # Copy the Go Modules manifests diff --git a/Makefile b/Makefile index 5592f2c..9733149 100644 --- a/Makefile +++ b/Makefile @@ -57,7 +57,7 @@ vet: ## Run go vet against code. .PHONY: test test: manifests generate fmt vet envtest ## Run tests. - KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out + KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test -v -p 1 ./... -coverprofile cover.out ##@ Build @@ -124,7 +124,7 @@ TMP_DIR=$$(mktemp -d) ;\ cd $$TMP_DIR ;\ go mod init tmp ;\ echo "Downloading $(2)" ;\ -GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\ +GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\ rm -rf $$TMP_DIR ;\ } endef diff --git a/chart/seeder-crd/templates/metal.harvesterhci.io_inventories.yaml b/chart/seeder-crd/templates/metal.harvesterhci.io_inventories.yaml index ab22830..5e003a1 100644 --- a/chart/seeder-crd/templates/metal.harvesterhci.io_inventories.yaml +++ b/chart/seeder-crd/templates/metal.harvesterhci.io_inventories.yaml @@ -89,12 +89,23 @@ spec: required: - connection type: object + events: + properties: + enabled: + type: boolean + pollingInterval: + default: 1h + type: string + required: + - enabled + type: object managementInterfaceMacAddress: type: string primaryDisk: type: string required: - baseboardSpec + - events - managementInterfaceMacAddress - primaryDisk type: object diff --git a/config/crd/bases/metal.harvesterhci.io_inventories.yaml b/config/crd/bases/metal.harvesterhci.io_inventories.yaml index 5e003a1..8be59ea 100644 --- a/config/crd/bases/metal.harvesterhci.io_inventories.yaml +++ b/config/crd/bases/metal.harvesterhci.io_inventories.yaml @@ -92,6 +92,7 @@ spec: events: properties: enabled: + default: false type: boolean pollingInterval: default: 1h diff --git a/main.go b/main.go index 6b1c4ab..91228ac 100644 --- a/main.go +++ b/main.go @@ -126,9 +126,19 @@ func main() { } if err = (&controllers.InventoryEventReconciller{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + Logger: log.FromContext(ctx).WithName("inventory- event-controller"), + EventRecorder: mgr.GetEventRecorderFor("seeder"), + }).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "InventoryEvent") + os.Exit(1) + } + + if err = (&controllers.ClusterEventReconciler{ Client: mgr.GetClient(), Scheme: mgr.GetScheme(), - Logger: log.FromContext(ctx).WithName("inventoryevent-controller"), + Logger: log.FromContext(ctx).WithName("cluster-event-controller"), EventRecorder: mgr.GetEventRecorderFor("seeder"), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "InventoryEvent") diff --git a/pkg/api/v1alpha1/common.go b/pkg/api/v1alpha1/common.go index 011ce5a..4ff1552 100644 --- a/pkg/api/v1alpha1/common.go +++ b/pkg/api/v1alpha1/common.go @@ -4,7 +4,7 @@ package v1alpha1 const ( DefaultNS = "metal-system" TinkConfig = "tinkerbell" - DefaultAPIPort = "6443" + DefaultAPIPort = "9345" OverrideAPIPortLabel = "clusterPort.harvesterhci.io" ) diff --git a/pkg/api/v1alpha1/inventory_types.go b/pkg/api/v1alpha1/inventory_types.go index c44003c..c0471ba 100644 --- a/pkg/api/v1alpha1/inventory_types.go +++ b/pkg/api/v1alpha1/inventory_types.go @@ -91,6 +91,7 @@ type Conditions struct { } type Events struct { + // +kubebuilder:default=false Enabled bool `json:"enabled"` // +kubebuilder:default:="1h" PollingInterval string `json:"pollingInterval,omitempty"` diff --git a/pkg/controllers/cluster_controller.go b/pkg/controllers/cluster_controller.go index 2caeb2f..bd56091 100644 --- a/pkg/controllers/cluster_controller.go +++ b/pkg/controllers/cluster_controller.go @@ -552,7 +552,7 @@ func genCoreTypedClient(ctx context.Context, c *seederv1alpha1.Cluster) (*typedC port = seederv1alpha1.DefaultAPIPort } - kcBytes, err := util.GenerateKubeConfig(fmt.Sprintf("https://%s:%s", c.Status.ClusterAddress, port), seederv1alpha1.DefaultAPIPrefix, c.Status.ClusterToken) + kcBytes, err := util.GenerateKubeConfig(c.Status.ClusterAddress, port, seederv1alpha1.DefaultAPIPrefix, c.Status.ClusterToken) if err != nil { return nil, err } diff --git a/pkg/events/events.go b/pkg/events/events.go index 82aa9b0..381211e 100644 --- a/pkg/events/events.go +++ b/pkg/events/events.go @@ -2,7 +2,6 @@ package events import ( "context" - "fmt" "strings" "github.com/stmcginnis/gofish" @@ -35,7 +34,6 @@ func (ef *EventFetcher) GetConfig() (map[string]string, string, error) { return nil, "", err } - var totalMemory, totalCoreCount, totalCoreHz int var manufacturer, model, serialNumber, health string for _, v := range chassis { if v.Manufacturer == "" { @@ -45,38 +43,14 @@ func (ef *EventFetcher) GetConfig() (map[string]string, string, error) { model = v.Model serialNumber = v.SerialNumber health = string(v.Status.Health) - cs, err := v.ComputerSystems() - if err != nil { - return nil, "", err - } - for _, c := range cs { - memory, err := c.Memory() - if err != nil { - return nil, "", err - } - - for _, m := range memory { - totalMemory += m.CapacityMiB - } - processors, err := c.Processors() - if err != nil { - return nil, "", err - } - for _, p := range processors { - totalCoreCount += p.TotalCores - totalCoreHz += int(p.MaxSpeedMHz) - } - } } retMap := make(map[string]string) - retMap["totalCpuCores"] = fmt.Sprintf("%d", totalCoreCount) - retMap["totalMemoryMiB"] = fmt.Sprintf("%d", totalMemory) - retMap["totalCoreHz"] = fmt.Sprintf("%d", totalCoreHz) retMap["manufacturer"] = strings.ReplaceAll(strings.ReplaceAll(manufacturer, " ", ""), ".", "") retMap["model"] = strings.ReplaceAll(strings.ReplaceAll(model, " ", ""), ".", "") retMap["serialNumber"] = strings.ReplaceAll(strings.ReplaceAll(serialNumber, " ", ""), ".", "") + return retMap, health, nil } diff --git a/pkg/util/client.go b/pkg/util/client.go index f86b09e..c3f9ee3 100644 --- a/pkg/util/client.go +++ b/pkg/util/client.go @@ -7,6 +7,7 @@ import ( "encoding/base64" "encoding/json" "fmt" + seederv1alpha1 "github.com/harvester/seeder/pkg/api/v1alpha1" "io/ioutil" "net/http" "time" @@ -26,7 +27,7 @@ type Config struct { } // Generate kubeconfig impersontates as a server and renders an admin kubeconfig which can be used to monitor and patch clusters -func GenerateKubeConfig(serverURL, prefix, token string) ([]byte, error) { +func GenerateKubeConfig(serverURL, port, prefix, token string) ([]byte, error) { c := &http.Client{Transport: &http.Transport{ IdleConnTimeout: 30 * time.Second, @@ -34,9 +35,9 @@ func GenerateKubeConfig(serverURL, prefix, token string) ([]byte, error) { InsecureSkipVerify: true, }, }, - Timeout: 30 * time.Second} + Timeout: 5 * time.Second} - configURL := fmt.Sprintf("%s/v1-%s/server-bootstrap", serverURL, prefix) + configURL := fmt.Sprintf("https://%s:%s/v1-%s/server-bootstrap", serverURL, port, prefix) req, err := http.NewRequest("GET", configURL, nil) if err != nil { return nil, err @@ -88,11 +89,21 @@ func GenerateKubeConfig(serverURL, prefix, token string) ([]byte, error) { InternalCAKey: internalCAKeyByte, } - return renderKubeConfig(serverConfig, serverURL) + // override to assist with unit tests + apiPort := "6443" + if port != seederv1alpha1.DefaultAPIPort { + apiPort = port + } + return renderKubeConfig(serverConfig, serverURL, apiPort) } // GenerateKubeConfig will generate an admin kubeconfig using the serverconfig generated -func renderKubeConfig(c *Config, serverURL string) ([]byte, error) { +func renderKubeConfig(c *Config, serverURL, port string) ([]byte, error) { + // rke2 k8s api and registration ports are different + // 9345 for registration + // 6443 for apiserver + // need to change the port in serverURL + adminTemplateKey, err := certutil.NewPrivateKey() if err != nil { return nil, err @@ -126,7 +137,7 @@ func renderKubeConfig(c *Config, serverURL string) ([]byte, error) { cluster := clientcmdapi.NewCluster() cluster.CertificateAuthorityData = c.ServerCA - cluster.Server = serverURL + cluster.Server = fmt.Sprintf("https://%s:%s", serverURL, port) //cluster.InsecureSkipTLSVerify = true authInfo := clientcmdapi.NewAuthInfo() diff --git a/pkg/util/client_test.go b/pkg/util/client_test.go index ae07fb8..26bd54f 100644 --- a/pkg/util/client_test.go +++ b/pkg/util/client_test.go @@ -68,7 +68,7 @@ func TestMain(t *testing.M) { func Test_GenerateKubeConfig(t *testing.T) { assert := require.New(t) - c, err := GenerateKubeConfig(fmt.Sprintf("https://localhost:%s", port), "k3s", token) + c, err := GenerateKubeConfig("localhost", port, "k3s", token) assert.NoError(err, "expected no error during generation of kubeconfig") assert.NoError(err) k8sclient, err := clientcmd.NewClientConfigFromBytes(c)