248 lines
6.6 KiB
Go
248 lines
6.6 KiB
Go
// Copyright 2023 Cloudbase Solutions SRL
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
// not use this file except in compliance with the License. You may obtain
|
|
// a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
// License for the specific language governing permissions and limitations
|
|
// under the License.
|
|
|
|
package provider
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
|
|
"edp.buildth.ing/DevFW-CICD/garm-provider-edge-connect/config"
|
|
"edp.buildth.ing/DevFW-CICD/garm-provider-edge-connect/internal/client"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
|
|
|
execution "github.com/cloudbase/garm-provider-common/execution/v0.1.0"
|
|
"github.com/cloudbase/garm-provider-common/params"
|
|
)
|
|
|
|
var Version = "v0.0.0-unknown"
|
|
|
|
func NewEdgeConnectProvider(configPath, controllerID string) (execution.ExternalProvider, error) {
|
|
conf, err := config.NewConfig(configPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error loading config: %w", err)
|
|
}
|
|
|
|
client := client.EdgeConnect{
|
|
BaseURL: "https://hub.apps.edge.platform.mg3.mdb.osc.live",
|
|
HttpClient: &http.Client{},
|
|
Credentials: client.Credentials{
|
|
Username: os.Getenv("EDGEXR_USERNAME"),
|
|
Password: os.Getenv("EDGEXR_PASSWORD"),
|
|
},
|
|
}
|
|
|
|
return &edgeConnectProvider{
|
|
client: client,
|
|
controllerID: controllerID,
|
|
cfg: conf,
|
|
}, nil
|
|
}
|
|
|
|
type edgeConnectProvider struct {
|
|
client client.EdgeConnect
|
|
controllerID string
|
|
cfg *config.Config
|
|
}
|
|
|
|
// CreateInstance creates a new compute instance in the provider.
|
|
func (a *edgeConnectProvider) CreateInstance(ctx context.Context, bootstrapParams params.BootstrapInstance) (params.ProviderInstance, error) {
|
|
podv1 := corev1.Pod{
|
|
TypeMeta: metav1.TypeMeta{
|
|
Kind: "Pod",
|
|
APIVersion: "v1",
|
|
},
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: bootstrapParams.Name,
|
|
Labels: map[string]string{"run": bootstrapParams.Name},
|
|
},
|
|
Spec: corev1.PodSpec{
|
|
Containers: []corev1.Container{
|
|
corev1.Container{
|
|
Name: "mganter-test",
|
|
Image: "edp.buildth.ing/devfw-cicd/nginx",
|
|
ImagePullPolicy: "Always",
|
|
Ports: []corev1.ContainerPort{
|
|
corev1.ContainerPort{
|
|
Name: "HTTP",
|
|
ContainerPort: 80,
|
|
Protocol: "TCP",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
service := corev1.Service{
|
|
TypeMeta: metav1.TypeMeta{
|
|
Kind: "Service",
|
|
APIVersion: "v1",
|
|
},
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: bootstrapParams.Name,
|
|
},
|
|
Spec: corev1.ServiceSpec{
|
|
Type: corev1.ServiceTypeLoadBalancer,
|
|
Ports: []corev1.ServicePort{
|
|
corev1.ServicePort{
|
|
Name: "tcp80",
|
|
Protocol: "TCP",
|
|
Port: 80,
|
|
TargetPort: intstr.FromInt(80),
|
|
},
|
|
},
|
|
Selector: map[string]string{"run": bootstrapParams.Name},
|
|
},
|
|
}
|
|
|
|
podjson, err := json.Marshal(podv1)
|
|
if err != nil {
|
|
return params.ProviderInstance{}, err
|
|
}
|
|
|
|
servicejson, err := json.Marshal(service)
|
|
if err != nil {
|
|
return params.ProviderInstance{}, err
|
|
}
|
|
|
|
manifest := fmt.Sprintf("%s\n---\n%s", string(podjson), string(servicejson))
|
|
|
|
err = a.client.NewApp(ctx, client.NewAppInput{
|
|
Region: a.cfg.Region,
|
|
App: client.App{
|
|
Key: client.AppKey{
|
|
Organization: a.cfg.Organization,
|
|
Name: bootstrapParams.Name,
|
|
Version: "0.0.1",
|
|
},
|
|
Deployment: "kubernetes",
|
|
ImageType: "Docker",
|
|
ImagePath: "edp.buildth.ing/devfw-cicd/nginx",
|
|
AllowServerless: true,
|
|
ServerlessConfig: struct{}{},
|
|
DefaultFlavor: client.Flavor{
|
|
Name: "EU.small",
|
|
},
|
|
DeploymentGenerator: "kubernetes-basic",
|
|
DeploymentManifest: manifest,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return params.ProviderInstance{}, err
|
|
}
|
|
|
|
err = a.client.NewAppInstance(ctx, client.NewAppInstanceInput{
|
|
Region: a.cfg.Region,
|
|
AppInst: client.AppInstance{
|
|
Key: client.AppInstanceKey{
|
|
Organization: a.cfg.Organization,
|
|
Name: bootstrapParams.Name,
|
|
CloudletKey: client.CloudletKey(a.cfg.CloudletKey),
|
|
},
|
|
AppKey: client.AppKey{
|
|
Organization: a.cfg.Organization,
|
|
Name: bootstrapParams.Name,
|
|
Version: "0.0.1",
|
|
},
|
|
Flavor: client.Flavor{
|
|
Name: "EU.small",
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return params.ProviderInstance{}, err
|
|
}
|
|
|
|
instance := params.ProviderInstance{
|
|
ProviderID: a.controllerID,
|
|
Name: bootstrapParams.Name,
|
|
OSType: params.Linux,
|
|
OSArch: params.Amd64,
|
|
OSName: "lala",
|
|
OSVersion: "lalatest",
|
|
Status: params.InstanceCreating,
|
|
}
|
|
|
|
return instance, nil
|
|
}
|
|
|
|
// Delete instance will delete the instance in a provider.
|
|
func (a *edgeConnectProvider) DeleteInstance(ctx context.Context, instance string) error {
|
|
return nil
|
|
}
|
|
|
|
// GetInstance will return details about one instance.
|
|
func (a *edgeConnectProvider) GetInstance(ctx context.Context, instance string) (params.ProviderInstance, error) {
|
|
providerInstance := params.ProviderInstance{
|
|
ProviderID: a.controllerID,
|
|
Name: instance,
|
|
OSType: params.Linux,
|
|
OSArch: params.Amd64,
|
|
OSName: "lala",
|
|
OSVersion: "lalatest",
|
|
Status: params.InstanceStatusUnknown,
|
|
}
|
|
|
|
appinstkey := client.AppInstanceKey{
|
|
Organization: a.cfg.Organization,
|
|
Name: instance,
|
|
CloudletKey: client.CloudletKey{
|
|
Organization: a.cfg.CloudletKey.Organization,
|
|
Name: a.cfg.CloudletKey.Name,
|
|
},
|
|
}
|
|
|
|
appinst, err := a.client.ShowAppInstance(ctx, appinstkey, a.cfg.Region)
|
|
if err != nil {
|
|
return params.ProviderInstance{}, err
|
|
}
|
|
|
|
if appinst.State == "Ready" {
|
|
providerInstance.Status = params.InstanceRunning
|
|
}
|
|
|
|
return providerInstance, nil
|
|
}
|
|
|
|
// ListInstances will list all instances for a provider.
|
|
func (a *edgeConnectProvider) ListInstances(ctx context.Context, poolID string) ([]params.ProviderInstance, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
// RemoveAllInstances will remove all instances created by this provider.
|
|
func (a *edgeConnectProvider) RemoveAllInstances(ctx context.Context) error {
|
|
return nil
|
|
}
|
|
|
|
// Stop shuts down the instance.
|
|
func (a *edgeConnectProvider) Stop(ctx context.Context, instance string, force bool) error {
|
|
return nil
|
|
}
|
|
|
|
// Start boots up an instance.
|
|
func (a *edgeConnectProvider) Start(ctx context.Context, instance string) error {
|
|
return nil
|
|
}
|
|
|
|
// GetVersion returns the version of the provider.
|
|
func (a *edgeConnectProvider) GetVersion(ctx context.Context) string {
|
|
return Version
|
|
}
|