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

Support Rio to Harness v0 #183

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
132 changes: 132 additions & 0 deletions command/rio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright 2022 Harness, Inc.
//
// 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 command

import (
"context"
"flag"
"fmt"
"github.com/drone/go-convert/convert/rio"
"io/ioutil"
"log"
"os"

"github.com/google/subcommands"
)

type Rio struct {
name string
proj string
org string
repoName string
repoConn string
kubeName string
kubeConn string
dockerConn string
userGroup string
githubConn string

downgrade bool
beforeAfter bool
}

func (*Rio) Name() string { return "rio" }
func (*Rio) Synopsis() string { return "converts a rio pipeline" }
func (*Rio) Usage() string {
return `rio [-downgrade] <path to rio.yml>
`
}

func (c *Rio) SetFlags(f *flag.FlagSet) {
f.BoolVar(&c.downgrade, "downgrade", false, "downgrade to the legacy yaml format")
f.BoolVar(&c.beforeAfter, "before-after", false, "print the befor and after")

f.StringVar(&c.org, "org", "default", "harness organization")
f.StringVar(&c.proj, "project", "default", "harness project")
f.StringVar(&c.name, "pipeline", "default", "harness pipeline name")
f.StringVar(&c.repoConn, "repo-connector", "", "repository connector")
f.StringVar(&c.repoName, "repo-name", "", "repository name")
f.StringVar(&c.kubeConn, "kube-connector", "", "kubernetes connector")
f.StringVar(&c.kubeName, "kube-namespace", "", "kubernets namespace")
f.StringVar(&c.dockerConn, "docker-connector", "", "dockerhub connector")
f.StringVar(&c.userGroup, "notification-user-group", "", "notification-user-group")
f.StringVar(&c.githubConn, "github-connector", "", "github-connector")
}

func (c *Rio) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
path := f.Arg(0)

fmt.Println("# v1 harness yaml is not supported for Rio pipelines. Converting to v0...")
// if the user does not specify the path as
// a command line arg, assume the default path.
if path == "" {
path = ".rio.yml"
}

// open the rio yaml
before, err := ioutil.ReadFile(path)
if err != nil {
log.Println(err)
return subcommands.ExitFailure
}

// convert the pipeline yaml from the rio
// format to the harness yaml format.
converter := rio.New(
rio.WithDockerhub(c.dockerConn),
rio.WithKubernetes(c.kubeName, c.kubeConn),
rio.WithName(c.name),
rio.WithIdentifier(c.name),
rio.WithOrganization(c.org),
rio.WithProject(c.proj),
rio.WithNotifyUserGroup(c.userGroup),
rio.WithGithubConnector(c.githubConn),
)
after, err := converter.ConvertBytes(before)
if err != nil {
log.Println(err)
return subcommands.ExitFailure
}

// downgrade from the v1 harness yaml format
// to the v0 harness yaml format.
if c.downgrade {
fmt.Println("# downgrade for Rio pipeline is not supported")
//// downgrade to the v0 yaml
//d := downgrader.New(
// downgrader.WithCodebase(c.repoName, c.repoConn),
// downgrader.WithDockerhub(c.dockerConn),
// downgrader.WithKubernetes(c.kubeName, c.kubeName),
// downgrader.WithName(c.name),
// downgrader.WithOrganization(c.org),
// downgrader.WithProject(c.proj),
//)
//after, err = d.Downgrade(after)
//if err != nil {
// log.Println(err)
// return subcommands.ExitFailure
//}
}

if c.beforeAfter {
os.Stdout.WriteString("---\n")
os.Stdout.Write(before)
os.Stdout.WriteString("\n---\n")
}

os.Stdout.Write(after)

return subcommands.ExitSuccess
}
14 changes: 13 additions & 1 deletion convert/harness/yaml/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

package yaml

const (
DefaultDockerConnector = "account.harnessImage"
)

type WhenStatus string

const (
Expand Down Expand Up @@ -85,7 +89,7 @@ type Shell string
const (
ShellNone Shell = "None"
ShellBash = "Bash"
ShellPosix = "Shell"
ShellPosix = "Sh"
ShellPowershell = "Powershell"
)

Expand All @@ -97,3 +101,11 @@ const (
ImagePullIfNotPresent = "IfNotPresent"
ImagePullNever = "Never"
)

type InfraOs string

const (
InfraOsLinux InfraOs = "Linux"
InfraOsMac = "MacOS"
InfraOsWindows = "Windows"
)
50 changes: 38 additions & 12 deletions convert/harness/yaml/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ type (

// Pipeline defines a pipeline.
Pipeline struct {
ID string `json:"identifier,omitempty" yaml:"identifier,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Desc string `json:"description,omitempty" yaml:"description,omitempty"`
Account string `json:"accountIdentifier,omitempty" yaml:"accountIdentifier,omitempty"`
Project string `json:"projectIdentifier,omitempty" yaml:"projectIdentifier,omitempty"`
Org string `json:"orgIdentifier,omitempty" yaml:"orgIdentifier,omitempty"`
Props Properties `json:"properties,omitempty" yaml:"properties,omitempty"`
Stages []*Stages `json:"stages,omitempty" yaml:"stages"`
Variables []*Variable `json:"variables,omitempty" yaml:"variables,omitempty"`
ID string `json:"identifier,omitempty" yaml:"identifier,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Desc string `json:"description,omitempty" yaml:"description,omitempty"`
Account string `json:"accountIdentifier,omitempty" yaml:"accountIdentifier,omitempty"`
Project string `json:"projectIdentifier,omitempty" yaml:"projectIdentifier,omitempty"`
Org string `json:"orgIdentifier,omitempty" yaml:"orgIdentifier,omitempty"`
Props Properties `json:"properties,omitempty" yaml:"properties,omitempty"`
Stages []*Stages `json:"stages,omitempty" yaml:"stages"`
Variables []*Variable `json:"variables,omitempty" yaml:"variables,omitempty"`
Timeout string `json:"timeout,omitempty" yaml:"timeout,omitempty"`
NotificationRules []NotificationRules `json:"notificationRules,omitempty" yaml:"notificationRules,omitempty"`
}

// Properties defines pipeline properties.
Expand Down Expand Up @@ -69,10 +71,13 @@ type (
Spec *InfraSpec `json:"spec,omitempty" yaml:"spec,omitempty"`
}

// InfraSpec describes pipeline infastructure.
// InfraSpec describes pipeline infrastructure.
InfraSpec struct {
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
Conn string `json:"connectorRef,omitempty" yaml:"connectorRef,omitempty"`
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
Conn string `json:"connectorRef,omitempty" yaml:"connectorRef,omitempty"`
AutomountServiceToken bool `json:"automountServiceAccountToken,omitempty" yaml:"automountServiceAccountToken,omitempty"`
Os string `json:"os,omitempty" yaml:"os,omitempty"`
NodeSelector map[string]string `json:"nodeSelector,omitempty" yaml:"nodeSelector,omitempty"`
}

Platform struct {
Expand Down Expand Up @@ -134,4 +139,25 @@ type (
Memory BytesSize `json:"memory,omitempty" yaml:"memory,omitempty"`
CPU MilliSize `json:"cpu,omitempty" yaml:"cpu,omitempty"` // TODO
}

NotificationRules struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Id string `json:"identifier,omitempty" yaml:"identifier,omitempty"`
PipelineEvents []NotificationPipelineEvent `json:"pipelineEvents,omitempty" yaml:"pipelineEvents,omitempty"`
NotificationMethod NotificationMethod `json:"notificationMethod,omitempty" yaml:"notificationMethod,omitempty"`
Enabled bool `json:"enabled,omitempty" yaml:"enabled,omitempty"`
}

NotificationPipelineEvent struct {
Type string `json:"type,omitempty" yaml:"type,omitempty"`
}

NotificationMethod struct {
Type string `json:"type,omitempty" yaml:"type,omitempty"`
Spec NotificationSpec `json:"spec,omitempty" yaml:"spec,omitempty"`
}

NotificationSpec struct {
UserGroups []string `json:"userGroups,omitempty" yaml:"userGroups,omitempty"`
}
)
2 changes: 1 addition & 1 deletion convert/harness/yaml/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ type (
Reports []*Report `json:"reports,omitempty" yaml:"reports,omitempty"`
Resources *Resources `json:"resources,omitempty" yaml:"resources,omitempty"`
RunAsUser string `json:"runAsUser,omitempty" yaml:"runAsUser,omitempty"`
Tags map[string]string `json:"tags,omitempty" yaml:"tags,omitempty"`
Tags []string `json:"tags,omitempty" yaml:"tags,omitempty"`
Target string `json:"target,omitempty" yaml:"target,omitempty"`
}

Expand Down
Loading