Skip to content

Commit

Permalink
Merge branch 'main' into feat/maas
Browse files Browse the repository at this point in the history
  • Loading branch information
gboutry committed Feb 9, 2024
2 parents 41b30e3 + b1f7843 commit 2f7257f
Show file tree
Hide file tree
Showing 75 changed files with 5,002 additions and 1,716 deletions.
9 changes: 5 additions & 4 deletions cloud/etc/deploy-microceph/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ resource "juju_application" "microceph" {
units = length(var.machine_ids) # need to manage the number of units

charm {
name = "microceph"
channel = var.charm_microceph_channel
name = "microceph"
channel = var.charm_microceph_channel
revision = var.charm_microceph_revision
base = "[email protected]"
}

config = {
config = merge({
snap-channel = var.microceph_channel
}
}, var.charm_microceph_config)
}

# juju_offer.microceph_offer will be created
Expand Down
15 changes: 14 additions & 1 deletion cloud/etc/deploy-microceph/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,25 @@

variable "charm_microceph_channel" {
description = "Operator channel for microceph deployment"
type = string
default = "edge"
}

variable "charm_microceph_revision" {
description = "Operator channel revision for microceph deployment"
type = number
default = null
}

variable "charm_microceph_config" {
description = "Operator config for microceph deployment"
type = map(string)
default = {}
}

variable "microceph_channel" {
description = "K8S channel to deploy, not the operator channel"
default = "latest/stable"
default = "reef/stable"
}

variable "machine_ids" {
Expand Down
9 changes: 5 additions & 4 deletions cloud/etc/deploy-microk8s/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ resource "juju_application" "microk8s" {
units = length(var.machine_ids) # need to manage the number of units

charm {
name = "microk8s"
channel = var.charm_microk8s_channel
name = "microk8s"
channel = var.charm_microk8s_channel
revision = var.charm_microk8s_revision
base = "[email protected]"
}

config = {
config = merge({
channel = var.microk8s_channel
addons = join(" ", [for key, value in var.addons : "${key}:${value}"])
disable_cert_reissue = true
kubelet_serialize_image_pulls = false
skip_verify = true
}
}, var.charm_microk8s_config)
}
13 changes: 13 additions & 0 deletions cloud/etc/deploy-microk8s/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,22 @@

variable "charm_microk8s_channel" {
description = "Operator channel for microk8s deployment"
type = string
default = "legacy/stable"
}

variable "charm_microk8s_revision" {
description = "Operator channel revision for microk8s deployment"
type = number
default = null
}

variable "charm_microk8s_config" {
description = "Operator config for microk8s deployment"
type = map(string)
default = {}
}

variable "microk8s_channel" {
description = "K8S channel to deploy, not the operator channel"
default = "1.28-strict/stable"
Expand Down
9 changes: 5 additions & 4 deletions cloud/etc/deploy-openstack-hypervisor/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ resource "juju_application" "openstack-hypervisor" {
units = length(var.machine_ids) # need to manage the number of units

charm {
name = "openstack-hypervisor"
channel = var.charm_channel
name = "openstack-hypervisor"
channel = var.charm_channel
revision = var.charm_revision
base = "[email protected]"
}

config = {
config = merge({
snap-channel = var.snap_channel
}
}, var.charm_config)

}

Expand Down
12 changes: 12 additions & 0 deletions cloud/etc/deploy-openstack-hypervisor/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ variable "charm_channel" {
default = "2023.2/edge"
}

variable "charm_revision" {
description = "Charm channel revision to deploy openstack-hypervisor charm from"
type = number
default = null
}

variable "charm_config" {
description = "Charm config to deploy openstack-hypervisor charm from"
type = map(string)
default = {}
}

variable "openstack_model" {
description = "Name of OpenStack model."
type = string
Expand Down
7 changes: 5 additions & 2 deletions cloud/etc/deploy-sunbeam-machine/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ resource "juju_application" "sunbeam-machine" {
units = length(var.machine_ids) # need to manage the number of units

charm {
name = "sunbeam-machine"
channel = var.charm_channel
name = "sunbeam-machine"
channel = var.charm_channel
revision = var.charm_revision
base = "[email protected]"
}

config = var.charm_config

}
12 changes: 12 additions & 0 deletions cloud/etc/deploy-sunbeam-machine/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ variable "charm_channel" {
default = "latest/edge"
}

variable "charm_revision" {
description = "Charm channel revision to deploy openstack-hypervisor charm from"
type = number
default = null
}

variable "charm_config" {
description = "Charm config to deploy openstack-hypervisor charm from"
type = map(string)
default = {}
}

variable "machine_model" {
description = "Name of model to deploy sunbeam-machine into."
type = string
Expand Down
2 changes: 2 additions & 0 deletions sunbeam-microcluster/api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ var Endpoints = []rest.Endpoint{
jujuusersCmd,
jujuuserCmd,
configCmd,
manifestsCmd,
manifestCmd,
}
91 changes: 91 additions & 0 deletions sunbeam-microcluster/api/manifests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package api

import (
"encoding/json"
"net/http"
"net/url"

"github.com/canonical/lxd/lxd/response"
"github.com/canonical/lxd/shared/api"
"github.com/canonical/microcluster/rest"
"github.com/canonical/microcluster/state"
"github.com/gorilla/mux"

"github.com/canonical/snap-openstack/sunbeam-microcluster/api/types"
"github.com/canonical/snap-openstack/sunbeam-microcluster/sunbeam"
)

// /1.0/manifests endpoint.
var manifestsCmd = rest.Endpoint{
Path: "manifests",

Get: rest.EndpointAction{Handler: cmdManifestsGetAll, ProxyTarget: true, AllowUntrusted: true},
Post: rest.EndpointAction{Handler: cmdManifestsPost, ProxyTarget: true, AllowUntrusted: true},
}

// /1.0/manifests/<manifestid> endpoint.
// /1.0/manifests/latest will give the latest inserted manifest record
var manifestCmd = rest.Endpoint{
Path: "manifests/{manifestid}",

Get: rest.EndpointAction{Handler: cmdManifestGet, ProxyTarget: true, AllowUntrusted: true},
Delete: rest.EndpointAction{Handler: cmdManifestDelete, ProxyTarget: true, AllowUntrusted: true},
}

func cmdManifestsGetAll(s *state.State, _ *http.Request) response.Response {

manifests, err := sunbeam.ListManifests(s)
if err != nil {
return response.InternalError(err)
}

return response.SyncResponse(true, manifests)
}

func cmdManifestGet(s *state.State, r *http.Request) response.Response {
var manifestid string
manifestid, err := url.PathUnescape(mux.Vars(r)["manifestid"])
if err != nil {
return response.InternalError(err)
}
manifest, err := sunbeam.GetManifest(s, manifestid)
if err != nil {
if err, ok := err.(api.StatusError); ok {
if err.Status() == http.StatusNotFound {
return response.NotFound(err)
}
}
return response.InternalError(err)
}

return response.SyncResponse(true, manifest)
}

func cmdManifestsPost(s *state.State, r *http.Request) response.Response {
var req types.Manifest

err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
return response.InternalError(err)
}

err = sunbeam.AddManifest(s, req.ManifestID, req.Data)
if err != nil {
return response.InternalError(err)
}

return response.EmptySyncResponse
}

func cmdManifestDelete(s *state.State, r *http.Request) response.Response {
manifestid, err := url.PathUnescape(mux.Vars(r)["manifestid"])
if err != nil {
return response.SmartError(err)
}
err = sunbeam.DeleteManifest(s, manifestid)
if err != nil {
return response.InternalError(err)
}

return response.EmptySyncResponse
}
12 changes: 12 additions & 0 deletions sunbeam-microcluster/api/types/manifests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Package types provides shared types and structs.
package types

// Manifests holds list of manifest type
type Manifests []Manifest

// Manifest structure to hold manifest applytime and manifest data
type Manifest struct {
ManifestID string `json:"manifestid" yaml:"manifestid"`
AppliedDate string `json:"applieddate" yaml:"applieddate"`
Data string `json:"data" yaml:"data"`
}
119 changes: 119 additions & 0 deletions sunbeam-microcluster/database/manifest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package database

import (
"context"
"database/sql"
"fmt"
"net/http"

"github.com/canonical/lxd/shared/api"
"github.com/canonical/microcluster/cluster"
)

//go:generate -command mapper lxd-generate db mapper -t manifest.mapper.go
//go:generate mapper reset
//
//go:generate mapper stmt -d github.com/canonical/microcluster/cluster -e ManifestItem objects table=manifest
//go:generate mapper stmt -d github.com/canonical/microcluster/cluster -e ManifestItem objects-by-ManifestID table=manifest
//go:generate mapper stmt -d github.com/canonical/microcluster/cluster -e ManifestItem id table=manifest
//go:generate mapper stmt -d github.com/canonical/microcluster/cluster -e ManifestItem delete-by-ManifestID table=manifest

//
//go:generate mapper method -i -d github.com/canonical/microcluster/cluster -e ManifestItem GetMany table=manifest
//go:generate mapper method -i -d github.com/canonical/microcluster/cluster -e ManifestItem GetOne table=manifest
//go:generate mapper method -i -d github.com/canonical/microcluster/cluster -e ManifestItem ID table=manifest
//go:generate mapper method -i -d github.com/canonical/microcluster/cluster -e ManifestItem Exists table=manifest
//go:generate mapper method -i -d github.com/canonical/microcluster/cluster -e ManifestItem DeleteOne-by-ManifestID table=manifest

// ManifestItem is used to save the Sunbeam manifests provided by user.
// AppliedDate is saved as Timestamp in database but retreived as string
// Probable Bug: https://github.com/mattn/go-sqlite3/issues/951
type ManifestItem struct {
ID int
ManifestID string `db:"primary=yes"`
AppliedDate string
Data string
}

// ManifestItemFilter is a required struct for use with lxd-generate. It is used for filtering fields on database fetches.
type ManifestItemFilter struct {
ManifestID *string
}

var manifestItemCreate = cluster.RegisterStmt(`
INSERT INTO manifest (manifest_id, data)
VALUES (?, ?)
`)

var latestManifestItemObject = cluster.RegisterStmt(`
SELECT manifest.id, manifest.manifest_id, manifest.applied_date, manifest.data
FROM manifest
WHERE manifest.applied_date = (SELECT MAX(applied_date) FROM manifest)
`)

// CreateManifestItem adds a new ManifestItem to the database.
// generator: ManifestItem Create
func CreateManifestItem(ctx context.Context, tx *sql.Tx, object ManifestItem) (int64, error) {
// Check if a ManifestItem with the same key exists.
exists, err := ManifestItemExists(ctx, tx, object.ManifestID)
if err != nil {
return -1, fmt.Errorf("Failed to check for duplicates: %w", err)
}

if exists {
return -1, api.StatusErrorf(http.StatusConflict, "This \"manifest\" entry already exists")
}

args := make([]any, 2)

// Populate the statement arguments.
args[0] = object.ManifestID
args[1] = object.Data

// Prepared statement to use.
stmt, err := cluster.Stmt(tx, manifestItemCreate)
if err != nil {
return -1, fmt.Errorf("Failed to get \"manifestItemCreate\" prepared statement: %w", err)
}

// Execute the statement.
result, err := stmt.Exec(args...)
if err != nil {
return -1, fmt.Errorf("Failed to create \"manifest\" entry: %w", err)
}

id, err := result.LastInsertId()
if err != nil {
return -1, fmt.Errorf("Failed to fetch \"manifest\" entry ID: %w", err)
}

return id, nil
}

// GetLatestManifestItem returns the latest inserted record in manifest table.
func GetLatestManifestItem(ctx context.Context, tx *sql.Tx) (*ManifestItem, error) {
var err error

// Pick the prepared statement and arguments to use based on active criteria.
var sqlStmt *sql.Stmt

sqlStmt, err = cluster.Stmt(tx, latestManifestItemObject)
if err != nil {
return nil, fmt.Errorf("Failed to get \"manifestItemObjects\" prepared statement: %w", err)
}

// Result slice.
// objects := make([]ManifestItem, 0)
objects, err := getManifestItems(ctx, sqlStmt)
if err != nil {
return nil, fmt.Errorf("Failed to fetch from \"manifest\" table: %w", err)
}

objectsLen := len(objects)
switch objectsLen {
case 0:
return nil, api.StatusErrorf(http.StatusNotFound, "ManifestItem not found")
default:
return &objects[objectsLen-1], nil
}
}
Loading

0 comments on commit 2f7257f

Please sign in to comment.