Skip to content

Commit

Permalink
Merge pull request #3 from ibrokethecloud/events
Browse files Browse the repository at this point in the history
Initial changes for events
  • Loading branch information
ibrokethecloud authored Oct 7, 2022
2 parents 769dcfd + a49b543 commit 9e27078
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
11 changes: 11 additions & 0 deletions chart/seeder-crd/templates/metal.harvesterhci.io_inventories.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/crd/bases/metal.harvesterhci.io_inventories.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ spec:
events:
properties:
enabled:
default: false
type: boolean
pollingInterval:
default: 1h
Expand Down
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/v1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package v1alpha1
const (
DefaultNS = "metal-system"
TinkConfig = "tinkerbell"
DefaultAPIPort = "6443"
DefaultAPIPort = "9345"
OverrideAPIPortLabel = "clusterPort.harvesterhci.io"
)

Expand Down
1 change: 1 addition & 0 deletions pkg/api/v1alpha1/inventory_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
28 changes: 1 addition & 27 deletions pkg/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package events

import (
"context"
"fmt"
"strings"

"github.com/stmcginnis/gofish"
Expand Down Expand Up @@ -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 == "" {
Expand All @@ -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
}
23 changes: 17 additions & 6 deletions pkg/util/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
seederv1alpha1 "github.com/harvester/seeder/pkg/api/v1alpha1"
"io/ioutil"
"net/http"
"time"
Expand All @@ -26,17 +27,17 @@ 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,
TLSClientConfig: &tls.Config{
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9e27078

Please sign in to comment.