Skip to content

Commit

Permalink
feat(server): able to grab server id and ip from cac
Browse files Browse the repository at this point in the history
  • Loading branch information
hef committed Jun 27, 2022
1 parent 12c964a commit db10243
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 56 deletions.
24 changes: 14 additions & 10 deletions api/v1beta1/server_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package v1beta1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"net"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand All @@ -29,22 +28,27 @@ type ServerSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Foo is an example field of Server. Edit server_types.go to remove/update
Cpu int `json:"cpu,omitempty"`
Encryption bool `json:"encryption,omitempty"`
Ha bool `json:"ha,omitempty"`
Os string `json:"os,omitempty"`
Ram int `json:"ram,omitempty"`
Storage int `json:"storage,omitempty"`
// Number of CPUs [1 - 10]
Cpu int `json:"cpu,omitempty"`
// Enable Encryption [true|false]
Encryption bool `json:"encryption,omitempty"`
// Enable High Availability [true|false]
Ha bool `json:"ha,omitempty"`
// Operating System ["CentOS 7.9 64bit", "CentOS 8.3 64bit", "Debian 9.13 64Bit", "FreeBSD 12.2 64bit", "Ubuntu 18.04 LTS 64bit"]
Os string `json:"os,omitempty"`
//Ram in MB, valid values are [512, 1024, 1536, 2048, 2560, 3072, 4096, 5120, 6144, 7168, 8192]
Ram int `json:"ram,omitempty"`
// Storage to allocate, in GB, valid values are multiples of 10
Storage int `json:"storage,omitempty"`
}

// ServerStatus defines the observed state of Server
type ServerStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
Id string `json:"id,omitempty"`
Id int64 `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Ip net.IP `json:"ip,omitempty"`
Ip string `json:"ip,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
8 changes: 1 addition & 7 deletions api/v1beta1/zz_generated.deepcopy.go

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

21 changes: 11 additions & 10 deletions config/crd/bases/incubation.hef.github.io_servers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,25 @@ spec:
description: ServerSpec defines the desired state of Server
properties:
cpu:
description: Foo is an example field of Server. Edit server_types.go
to remove/update
description: Number of CPUs [1 - 10]
type: integer
encryption:
description: Enable Encryption [true|false]
type: boolean
ha:
description: Enable High Availability [true|false]
type: boolean
os:
description: Operating System ["CentOS 7.9 64bit", "CentOS 8.3 64bit",
"Debian 9.13 64Bit", "FreeBSD 12.2 64bit", "Ubuntu 18.04 LTS 64bit"]
type: string
ram:
description: Ram in MB, valid values are [512, 1024, 1536, 2048, 2560,
3072, 4096, 5120, 6144, 7168, 8192]
type: integer
storage:
description: Storage to allocate, in GB, valid values are multiples
of 10
type: integer
type: object
status:
Expand All @@ -57,15 +64,9 @@ spec:
description: 'INSERT ADDITIONAL STATUS FIELD - define observed state
of cluster Important: Run "make" to regenerate code after modifying
this file'
type: string
format: int64
type: integer
ip:
description: "An IP is a single IP address, a slice of bytes. Functions
in this package accept either 4-byte (IPv4) or 16-byte (IPv6) slices
as input. \n Note that in this documentation, referring to an IP
address as an IPv4 address or an IPv6 address is a semantic property
of the address, not just the length of the byte slice: a 16-byte
slice can still be an IPv4 address."
format: byte
type: string
name:
type: string
Expand Down
43 changes: 41 additions & 2 deletions controllers/server_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package controllers

import (
"context"
"k8s.io/utils/env"

cacctl "github.com/hef/cacctl/client"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -47,9 +49,46 @@ type ServerReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.1/pkg/reconcile
func (r *ServerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)
logger := log.FromContext(ctx)

// TODO(user): your logic here
x := incubationv1beta1.Server{}
err := r.Get(ctx, req.NamespacedName, &x)
if err != nil {
logger.Error(err, "unable to fetch Server")
return ctrl.Result{}, err
}

username := env.GetString("CAC_USERNAME", "")
password := env.GetString("CAC_PASSWORD", "")

client, err := cacctl.New(
cacctl.WithUsernameAndPassword(username, password),
cacctl.WithUserAgent("cac-operator/dev"),
)
if err != nil {
logger.Error(err, "error creating client")
return ctrl.Result{}, err
}

servers, err := client.ListWithFilter(ctx, req.Name, 200, cacctl.All)
if err != nil {
logger.Error(err, "error listing servers")
return ctrl.Result{}, err
}

for _, server := range servers.Servers {
if server.ServerName == req.Name {
x.Status.Ip = server.IpAddress.String()
x.Status.Id = server.ServerId
x.Name = server.ServerName
}
}

err = r.Status().Update(ctx, &x)
if err != nil {
logger.Error(err, "error updating server status")
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
}
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/hef/cac-operator
go 1.18

require (
github.com/hef/cacctl v0.16.0
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.19.0
k8s.io/apimachinery v0.24.2
Expand All @@ -11,15 +12,17 @@ require (
)

require (
cloud.google.com/go v0.81.0 // indirect
cloud.google.com/go v0.99.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest v0.11.18 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.13 // indirect
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/PuerkitoBio/goquery v1.8.0 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -36,7 +39,7 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
Expand Down
Loading

0 comments on commit db10243

Please sign in to comment.