Skip to content

Commit

Permalink
Get Service Functional (#2)
Browse files Browse the repository at this point in the history
Test and get working the Helm deployment, and start to expand the API to
be useful to the Kubernetes service.
  • Loading branch information
spjmurray authored Jun 6, 2024
1 parent 5b971d0 commit 54a2ad1
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 107 deletions.
2 changes: 1 addition & 1 deletion charts/region/crds/region.unikorn-cloud.org_regions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ spec:
listKind: RegionList
plural: regions
singular: region
scope: Cluster
scope: Namespaced
versions:
- additionalPrinterColumns:
- jsonPath: .metadata.labels['unikorn-cloud\.org/name']
Expand Down
14 changes: 1 addition & 13 deletions charts/region/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,8 @@ Create the container images
{{- end }}
{{- end }}

{{- define "unikorn.clusterManagerControllerImage" -}}
{{- .Values.clusterManagerController.image | default (printf "%s/unikorn-cluster-manager-controller:%s" (include "unikorn.defaultRepositoryPath" .) (.Values.tag | default .Chart.Version)) }}
{{- end }}

{{- define "unikorn.clusterControllerImage" -}}
{{- .Values.clusterController.image | default (printf "%s/unikorn-cluster-controller:%s" (include "unikorn.defaultRepositoryPath" .) (.Values.tag | default .Chart.Version)) }}
{{- end }}

{{- define "unikorn.monitorImage" -}}
{{- .Values.monitor.image | default (printf "%s/unikorn-monitor:%s" (include "unikorn.defaultRepositoryPath" .) (.Values.tag | default .Chart.Version)) }}
{{- end }}

{{- define "unikorn.regionImage" -}}
{{- .Values.region.image | default (printf "%s/unikorn-region:%s" (include "unikorn.defaultRepositoryPath" .) (.Values.tag | default .Chart.Version)) }}
{{- .Values.region.image | default (printf "%s/unikorn-region-controller:%s" (include "unikorn.defaultRepositoryPath" .) (.Values.tag | default .Chart.Version)) }}
{{- end }}

{{/*
Expand Down
36 changes: 1 addition & 35 deletions charts/region/templates/region-controller/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,16 @@ metadata:
rules:
# Orchestrate Unikorn resources (my job).
- apiGroups:
- unikorn-cloud.org
resources:
- clustermanagers
- kubernetesclusters
verbs:
- create
- get
- list
- watch
- patch
- delete
- apiGroups:
- unikorn-cloud.org
- region.unikorn-cloud.org
resources:
- regions
- clustermanagerapplicationbundles
- kubernetesclusterapplicationbundles
- helmapplications
verbs:
- list
- watch
# Find project namespaces
- apiGroups:
- ""
resources:
- namespaces
verbs:
- list
- watch
# Get secrets, ugh, for kubeconfigs.
- apiGroups:
- ""
resources:
- secrets
- services
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
- events
verbs:
- get
- list
- watch
1 change: 1 addition & 0 deletions charts/region/templates/region-controller/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ spec:
- name: unikorn-region
image: {{ include "unikorn.regionImage" . }}
args:
- --namespace={{ .Release.Namespace }}
{{- with $cors := .Values.region.cors }}
{{- range $origin := $cors.allowOrigin }}
{{ printf "- --cors-allow-origin=%s" $origin | nindent 8 }}
Expand Down
2 changes: 1 addition & 1 deletion charts/region/templates/region.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{- range $region := .Values.regions }}
apiVersion: unikorn-cloud.org/v1alpha1
apiVersion: region.unikorn-cloud.org/v1alpha1
kind: Region
metadata:
name: {{ include "resource.id" $region.name }}
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/unikorn/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type RegionList struct {
// about the provider for that region.
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:resource:scope=Cluster,categories=unikorn
// +kubebuilder:resource:scope=Namespaced,categories=unikorn
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="display name",type="string",JSONPath=".metadata.labels['unikorn-cloud\\.org/name']"
// +kubebuilder:printcolumn:name="provider",type="string",JSONPath=".spec.provider"
Expand Down
11 changes: 7 additions & 4 deletions pkg/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ type Handler struct {
// client gives cached access to Kubernetes.
client client.Client

// namespace is the namespace we are running in.
namespace string

// options allows behaviour to be defined on the CLI.
options *Options

// authorizerOptions allows access to the identity service for RBAC callbacks.
authorizerOptions *oidc.Options
}

func New(client client.Client, options *Options, authorizerOptions *oidc.Options) (*Handler, error) {
func New(client client.Client, namespace string, options *Options, authorizerOptions *oidc.Options) (*Handler, error) {
h := &Handler{
client: client,
options: options,
Expand All @@ -65,7 +68,7 @@ func (h *Handler) setUncacheable(w http.ResponseWriter) {
}

func (h *Handler) GetApiV1Regions(w http.ResponseWriter, r *http.Request) {
result, err := region.NewClient(h.client).List(r.Context())
result, err := region.NewClient(h.client, h.namespace).List(r.Context())
if err != nil {
errors.HandleError(w, r, err)
return
Expand All @@ -76,7 +79,7 @@ func (h *Handler) GetApiV1Regions(w http.ResponseWriter, r *http.Request) {
}

func (h *Handler) GetApiV1RegionsRegionIDFlavors(w http.ResponseWriter, r *http.Request, regionID openapi.RegionIDParameter) {
provider, err := region.NewClient(h.client).Provider(r.Context(), regionID)
provider, err := region.NewClient(h.client, h.namespace).Provider(r.Context(), regionID)
if err != nil {
errors.HandleError(w, r, err)
return
Expand Down Expand Up @@ -113,7 +116,7 @@ func (h *Handler) GetApiV1RegionsRegionIDFlavors(w http.ResponseWriter, r *http.
}

func (h *Handler) GetApiV1RegionsRegionIDImages(w http.ResponseWriter, r *http.Request, regionID openapi.RegionIDParameter) {
provider, err := region.NewClient(h.client).Provider(r.Context(), regionID)
provider, err := region.NewClient(h.client, h.namespace).Provider(r.Context(), regionID)
if err != nil {
errors.HandleError(w, r, err)
return
Expand Down
23 changes: 18 additions & 5 deletions pkg/handler/region/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ var (
)

type Client struct {
client client.Client
// region string
client client.Client
namespace string
}

func NewClient(client client.Client) *Client {
func NewClient(client client.Client, namespace string) *Client {
return &Client{
client: client,
client: client,
namespace: namespace,
}
}

Expand All @@ -55,7 +56,7 @@ func NewClient(client client.Client) *Client {
func (c *Client) list(ctx context.Context) (*unikornv1.RegionList, error) {
var regions unikornv1.RegionList

if err := c.client.List(ctx, &regions); err != nil {
if err := c.client.List(ctx, &regions, &client.ListOptions{Namespace: c.namespace}); err != nil {
return nil, err
}

Expand Down Expand Up @@ -110,9 +111,21 @@ func (c *Client) Provider(ctx context.Context, regionName string) (providers.Pro
return provider, nil
}

func convertRegionType(in unikornv1.Provider) openapi.RegionType {
switch in {
case unikornv1.ProviderOpenstack:
return openapi.Openstack
}

return ""
}

func convert(in *unikornv1.Region) *openapi.RegionRead {
out := &openapi.RegionRead{
Metadata: conversion.ResourceReadMetadata(in, coreopenapi.ResourceProvisioningStatusProvisioned),
Spec: openapi.RegionSpec{
Type: convertRegionType(in.Spec.Provider),
},
}

return out
Expand Down
93 changes: 47 additions & 46 deletions pkg/openapi/schema.go

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

18 changes: 18 additions & 0 deletions pkg/openapi/server.spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,30 @@ components:
type: string
minLength: 1
maxLength: 63
regionType:
description: The region's provider type.
type: string
enum:
- openstack
regionSpec:
description: Information about the region.
type: object
required:
- type
properties:
type:
$ref: '#/components/schemas/regionType'
regionRead:
description: A region.
type: object
required:
- metadata
- spec
properties:
metadata:
$ref: 'https://raw.githubusercontent.com/unikorn-cloud/core/main/pkg/openapi/common.spec.yaml#/components/schemas/resourceReadMetadata'
spec:
$ref: '#/components/schemas/regionSpec'
regions:
description: A list of regions.
type: array
Expand Down Expand Up @@ -185,6 +201,8 @@ components:
description: An oxymoronic tier-3 datacenter based in Liverpool.
creationTime: 2023-07-31T10:45:45Z
provisioningStatus: provisioned
spec:
type: openstack
imagesResponse:
description: A list of images that are compatible with this platform.
content:
Expand Down
17 changes: 17 additions & 0 deletions pkg/openapi/types.go

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

3 changes: 3 additions & 0 deletions pkg/server/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (

// Options allows server options to be overridden.
type Options struct {
Namespace string

// ListenAddress tells the server what to listen on, you shouldn't
// need to change this, its already non-privileged and the default
// should be modified to avoid clashes with other services e.g prometheus.
Expand Down Expand Up @@ -55,6 +57,7 @@ type Options struct {

// addFlags allows server options to be modified.
func (o *Options) AddFlags(f *pflag.FlagSet) {
f.StringVar(&o.Namespace, "namespace", "", "The namespace the service is running in.")
f.StringVar(&o.ListenAddress, "server-listen-address", ":6080", "API listener address.")
f.DurationVar(&o.ReadTimeout, "server-read-timeout", time.Second, "How long to wait for the client to send the request body.")
f.DurationVar(&o.ReadHeaderTimeout, "server-read-header-timeout", time.Second, "How long to wait for the client to send headers.")
Expand Down
Loading

0 comments on commit 54a2ad1

Please sign in to comment.