Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init piccolo #503

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ replace github.com/eclipse-symphony/symphony/coa => ../coa

replace github.com/eclipse-symphony/symphony/packages/mage => ../packages/mage

replace "github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/providers/target/piccolo" => ./pkg/apis/v1alpha1/providers/target/piccolo

require (
github.com/eclipse-symphony/symphony/coa v0.0.0
github.com/spf13/cobra v1.8.1
Expand Down
15 changes: 15 additions & 0 deletions api/pkg/apis/v1alpha1/providers/providerfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/providers/target/kubectl"
tgtmock "github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/providers/target/mock"
"github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/providers/target/mqtt"
"github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/providers/target/piccolo"
"github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/providers/target/proxy"
"github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/providers/target/script"
"github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/providers/target/staging"
Expand Down Expand Up @@ -195,6 +196,12 @@ func (s SymphonyProviderFactory) CreateProvider(providerType string, config cp.I
if err == nil {
return mProvider, nil
}
case "providers.target.piccolo":
mProvider := &piccolo.PiccoloTargetProvider{}
err = mProvider.Init(config)
if err == nil {
return mProvider, nil
}
case "providers.target.ingress":
mProvider := &ingress.IngressTargetProvider{}
err = mProvider.Init(config)
Expand Down Expand Up @@ -414,6 +421,14 @@ func CreateProviderForTargetRole(context *contexts.ManagerContext, role string,
}
provider.Context = context
return provider, nil
case "providers.target.piccolo":
provider := &piccolo.PiccoloTargetProvider{}
err := provider.InitWithMap(binding.Config)
if err != nil {
return nil, err
}
provider.Context = context
return provider, nil
case "providers.target.ingress":
provider := &ingress.IngressTargetProvider{}
err := provider.InitWithMap(binding.Config)
Expand Down
14 changes: 14 additions & 0 deletions api/pkg/apis/v1alpha1/providers/providerfactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/providers/target/k8s"
"github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/providers/target/kubectl"
tgtmock "github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/providers/target/mock"
"github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/providers/target/piccolo"
"github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/providers/target/proxy"
"github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/providers/target/script"
"github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/providers/target/staging"
Expand Down Expand Up @@ -158,6 +159,10 @@ func TestCreateProvider(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, *provider.(*docker.DockerTargetProvider))

provider, err = providerfactory.CreateProvider("providers.target.piccolo", piccolo.PiccoloTargetProviderConfig{})
assert.Nil(t, err)
assert.NotNil(t, *provider.(*piccolo.PiccoloTargetProvider))

if getTestMiniKubeEnabled == "" {
t.Log("Skipping providers.target.ingress test as TEST_MINIKUBE_ENABLED is not set")
} else {
Expand Down Expand Up @@ -348,6 +353,11 @@ func TestCreateProviderForTargetRole(t *testing.T) {
Provider: "providers.target.docker",
Config: map[string]string{},
},
{
Role: "piccolo",
Provider: "providers.target.piccolo",
Config: map[string]string{},
},
{
Role: "ingress",
Provider: "providers.target.ingress",
Expand Down Expand Up @@ -634,6 +644,10 @@ func TestCreateProviderForTargetRole(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, *provider.(*docker.DockerTargetProvider))

provider, err = CreateProviderForTargetRole(nil, "piccolo", targetState, nil)
assert.Nil(t, err)
assert.NotNil(t, *provider.(*piccolo.PiccoloTargetProvider))

if getTestMiniKubeEnabled == "" {
t.Log("Skipping ingress test as TEST_MINIKUBE_ENABLED is not set")
} else {
Expand Down
226 changes: 226 additions & 0 deletions api/pkg/apis/v1alpha1/providers/target/piccolo/piccolo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
* SPDX-License-Identifier: MIT
*/

package piccolo

import (
"bytes"
"context"
"encoding/json"
"errors"
"net/http"

"github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/model"
"github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2"
"github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2/contexts"
"github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2/observability"
observ_utils "github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2/observability/utils"
"github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2/providers"
"github.com/eclipse-symphony/symphony/coa/pkg/logger"
)

const loggerName = "providers.target.piccolo"
const defaultPiccoloApiServer = "http://0.0.0.0:9090/"

var sLog = logger.NewLogger(loggerName)

type PiccoloTargetProviderConfig struct {
Name string `json:"name"`
Url string `json:"url"`
}

type PiccoloTargetProvider struct {
Config PiccoloTargetProviderConfig
Context *contexts.ManagerContext
}

func PiccoloTargetProviderConfigFromMap(properties map[string]string) (PiccoloTargetProviderConfig, error) {
ret := PiccoloTargetProviderConfig{}
if v, ok := properties["name"]; ok {
ret.Name = v
}
if v, ok := properties["url"]; ok {
ret.Url = v
} else {
ret.Url = defaultPiccoloApiServer
}
return ret, nil
}
func (d *PiccoloTargetProvider) InitWithMap(properties map[string]string) error {
config, err := PiccoloTargetProviderConfigFromMap(properties)
if err != nil {
return err
}
return d.Init(config)
}
func (s *PiccoloTargetProvider) SetContext(ctx *contexts.ManagerContext) {
s.Context = ctx
}

func (d *PiccoloTargetProvider) Init(config providers.IProviderConfig) error {
_, span := observability.StartSpan("Piccolo Target Provider", context.TODO(), &map[string]string{
"method": "Init",
})
var err error = nil
defer observ_utils.CloseSpanWithError(span, &err)

sLog.Info(" P (Piccolo Target): Init()")

// convert config to PiccoloTargetProviderConfig type
piccoloConfig, err := toPiccoloTargetProviderConfig(config)
if err != nil {
sLog.Errorf(" P (Piccolo Target): expected PiccoloTargetProviderConfig: %+v", err)
return err
}

d.Config = piccoloConfig
return nil
}

func toPiccoloTargetProviderConfig(config providers.IProviderConfig) (PiccoloTargetProviderConfig, error) {
ret := PiccoloTargetProviderConfig{}
data, err := json.Marshal(config)
if err != nil {
return ret, err
}
err = json.Unmarshal(data, &ret)
return ret, err
}

func (i *PiccoloTargetProvider) Get(ctx context.Context, deployment model.DeploymentSpec, references []model.ComponentStep) ([]model.ComponentSpec, error) {
ctx, span := observability.StartSpan("Piccolo Target Provider", ctx, &map[string]string{
"method": "Get",
})
var err error = nil
defer observ_utils.CloseSpanWithError(span, &err)

sLog.InfofCtx(ctx, " P (Piccolo Target): getting artifacts: %s - %s, traceId: %s", deployment.Instance.Spec.Scope, deployment.Instance.ObjectMeta.Name, span.SpanContext().TraceID().String())

ret := make([]model.ComponentSpec, 0)
for _, component := range references {
properties := component.Component.Properties
name := properties["workload.name"].(string)

req, err := http.NewRequest("GET", i.Config.Url+"scenario/"+name, nil)
if err != nil {
sLog.ErrorCtx(ctx, " P (Piccolo Target): Unable to make Request")
return nil, err
}
client := &http.Client{}
resp, err := client.Do(req)

if err != nil {
sLog.ErrorfCtx(ctx, " P (Piccolo Target): Unable to get workload %s from piccolo", name)
return nil, err
}

defer resp.Body.Close()

switch resp.StatusCode {
case http.StatusOK:
// respBody, err := io.ReadAll(resp.Body)
component := model.ComponentSpec{
Name: name,
Properties: make(map[string]interface{}),
}
component.Properties["workload.name"] = string(name)
ret = append(ret, component)
case http.StatusNotFound:
continue
}
}

return ret, nil
}

func (i *PiccoloTargetProvider) Apply(ctx context.Context, deployment model.DeploymentSpec, step model.DeploymentStep, isDryRun bool) (map[string]model.ComponentResultSpec, error) {
ctx, span := observability.StartSpan("Piccolo Target Provider", ctx, &map[string]string{
"method": "Apply",
})
var err error = nil
defer observ_utils.CloseSpanWithError(span, &err)

sLog.InfofCtx(ctx, " P (Piccolo Target): applying artifacts: %s - %s, traceId: %s", deployment.Instance.Spec.Scope, deployment.Instance.ObjectMeta.Name, span.SpanContext().TraceID().String())

injections := &model.ValueInjections{
InstanceId: deployment.Instance.ObjectMeta.Name,
SolutionId: deployment.Instance.Spec.Solution,
TargetId: deployment.ActiveTarget,
}

components := step.GetComponents()
err = i.GetValidationRule(ctx).Validate(components)
if err != nil {
sLog.ErrorfCtx(ctx, " P (Piccolo Target): failed to validate components: %+v, traceId: %s", err, span.SpanContext().TraceID().String())
return nil, err
}
if isDryRun {
err = nil
return nil, nil
}

ret := step.PrepareResultMap()

for _, component := range step.Components {
if component.Action == model.ComponentUpdate {
name := model.ReadPropertyCompat(component.Component.Properties, "workload.name", injections)
if name == "" {
err = errors.New("component doesn't have workload.name property")
ret[component.Component.Name] = model.ComponentResultSpec{
Status: v1alpha2.UpdateFailed,
Message: err.Error(),
}
sLog.ErrorfCtx(ctx, " P (Piccolo Target): %+v, traceId: %s", err, span.SpanContext().TraceID().String())
return ret, err
}
reqBody := bytes.NewBufferString("https:// scenario path")
resp, err := http.Post(i.Config.Url+"create-scenario/"+name, "text/plain", reqBody)
if err != nil {
sLog.ErrorCtx(ctx, " P (Piccolo Target): fail to create resource")
return ret, err
}

defer resp.Body.Close()

ret[component.Component.Name] = model.ComponentResultSpec{
Status: v1alpha2.Updated,
Message: "",
}
} else {
req, err := http.NewRequest("DELETE", i.Config.Url+"delete-scenario/"+component.Component.Name, nil)
if err != nil {
return ret, err
}

client := &http.Client{}
_, err = client.Do(req)

if err == nil {
ret[component.Component.Name] = model.ComponentResultSpec{
Status: v1alpha2.Deleted,
Message: "",
}
}
}
}
return ret, nil
}

func (*PiccoloTargetProvider) GetValidationRule(ctx context.Context) model.ValidationRule {
return model.ValidationRule{
AllowSidecar: false,
ComponentValidationRule: model.ComponentValidationRule{
RequiredProperties: []string{"workload.name"},
OptionalProperties: []string{},
RequiredComponentType: "",
RequiredMetadata: []string{},
OptionalMetadata: []string{},
ChangeDetectionProperties: []model.PropertyDesc{
{Name: "workload.name", IgnoreCase: false, SkipIfMissing: false},
},
},
}
}
Loading
Loading