Skip to content

Commit

Permalink
Merge branch 'main' into issue-2790
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Sonefors committed Sep 27, 2022
2 parents a5c6b34 + 0b36fcc commit 38e3e8d
Show file tree
Hide file tree
Showing 19 changed files with 1,418 additions and 515 deletions.
8 changes: 4 additions & 4 deletions api/core/core.proto
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ message ListEventsResponse {
}

message SyncFluxObjectRequest {
repeated ClusteredObjRef objects = 1;
bool withSource = 2;
repeated ObjectRef objects = 1;
bool withSource = 2;
}

message SyncFluxObjectResponse {
Expand All @@ -264,8 +264,8 @@ message GetFeatureFlagsResponse {
}

message ToggleSuspendResourceRequest {
repeated ClusteredObjRef objects = 1;
bool suspend = 2;
repeated ObjectRef objects = 1;
bool suspend = 2;
}

message ToggleSuspendResourceResponse {
Expand Down
30 changes: 11 additions & 19 deletions api/core/core.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@
"in": "query",
"required": false,
"type": "string"
},
{
"name": "involvedObject.clusterName",
"in": "query",
"required": false,
"type": "string"
}
],
"tags": [
Expand Down Expand Up @@ -493,23 +499,6 @@
}
}
},
"v1ClusteredObjRef": {
"type": "object",
"properties": {
"kind": {
"type": "string"
},
"name": {
"type": "string"
},
"namespace": {
"type": "string"
},
"clusterName": {
"type": "string"
}
}
},
"v1Condition": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -888,6 +877,9 @@
},
"namespace": {
"type": "string"
},
"clusterName": {
"type": "string"
}
}
},
Expand All @@ -897,7 +889,7 @@
"objects": {
"type": "array",
"items": {
"$ref": "#/definitions/v1ClusteredObjRef"
"$ref": "#/definitions/v1ObjectRef"
}
},
"withSource": {
Expand All @@ -914,7 +906,7 @@
"objects": {
"type": "array",
"items": {
"$ref": "#/definitions/v1ClusteredObjRef"
"$ref": "#/definitions/v1ObjectRef"
}
},
"suspend": {
Expand Down
6 changes: 0 additions & 6 deletions api/core/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ message ObjectRef {
string kind = 1;
string name = 2;
string namespace = 3;
}

message ClusteredObjRef {
string kind = 1;
string name = 2;
string namespace = 3;
string clusterName = 4;
}

Expand Down
78 changes: 74 additions & 4 deletions cmd/gitops/beta/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package run
import (
"context"
"fmt"
"github.com/weaveworks/weave-gitops/pkg/fluxexec"
"github.com/weaveworks/weave-gitops/pkg/fluxinstall"
"os"
"os/signal"
"path/filepath"
Expand All @@ -13,6 +11,9 @@ import (
"syscall"
"time"

"github.com/weaveworks/weave-gitops/pkg/fluxexec"
"github.com/weaveworks/weave-gitops/pkg/fluxinstall"

"github.com/fsnotify/fsnotify"
"github.com/manifoldco/promptui"
"github.com/minio/minio-go/v7"
Expand Down Expand Up @@ -223,7 +224,9 @@ func betaRunCommandRunE(opts *config.Options) func(*cobra.Command, []string) err

log.Actionf("Checking if Flux is already installed ...")

if fluxVersion, err := run.GetFluxVersion(log, ctx, kubeClient); err != nil {
fluxVersion := ""

if fluxVersion, err = run.GetFluxVersion(log, ctx, kubeClient); err != nil {
log.Warningf("Flux is not found: %v", err.Error())

product := fluxinstall.NewProduct(flags.FluxVersion)
Expand Down Expand Up @@ -500,7 +503,7 @@ func betaRunCommandRunE(opts *config.Options) func(*cobra.Command, []string) err

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
sig := <-sigs

if err := watcher.Close(); err != nil {
log.Warningf("Error closing watcher: %v", err.Error())
Expand All @@ -525,6 +528,73 @@ func betaRunCommandRunE(opts *config.Options) func(*cobra.Command, []string) err
return err
}

// run bootstrap wizard only if Flux is not installed and env var is set
if fluxVersion != "" || os.Getenv("GITOPS_RUN_BOOTSTRAP") == "" {
return nil
}

// re-enable listening for ctrl+C
signal.Reset(sig)

// parse remote
repo, err := run.ParseGitRemote(log, rootDir)
if err != nil {
log.Failuref("Error parsing Git remote: %v", err.Error())
}

// run the bootstrap wizard
log.Actionf("Starting bootstrap wizard ...")

log.Waitingf("Press Ctrl+C to stop bootstrap wizard ...")

remoteURL, err := run.ParseRemoteURL(repo)
if err != nil {
log.Failuref("Error parsing remote URL: %v", err.Error())
}

var gitProvider run.GitProvider

if remoteURL == "" {
gitProvider, err = run.SelectGitProvider(log)
if err != nil {
log.Failuref("Error selecting git provider: %v", err.Error())
}
} else {
urlParts := run.GetURLParts(remoteURL)

if len(urlParts) > 0 {
gitProvider = run.ParseGitProvider(urlParts[0])
}
}

if gitProvider == run.GitProviderUnknown {
gitProvider, err = run.SelectGitProvider(log)
if err != nil {
log.Failuref("Error selecting git provider: %v", err.Error())
}
}

relativePathForBootstrapWizard, err := run.GetRelativePathToRootDir(rootDir, targetPath)
if err != nil { // if there is no git repo, we return an error
return err
}

path := filepath.Join(relativePathForBootstrapWizard, "clusters", "my-cluster")
path = "./" + path

wizard, err := run.NewBootstrapWizard(log, remoteURL, gitProvider, repo, path)
if err != nil {
return fmt.Errorf("error creating bootstrap wizard: %v", err.Error())
}

if err = wizard.Run(log); err != nil {
return fmt.Errorf("error running bootstrap wizard: %v", err.Error())
}

fluxBootstrapCmd := wizard.BuildCommand(log)

log.Successf("Flux bootstrap cmd:\n%s", fluxBootstrapCmd)

return nil
}
}
21 changes: 15 additions & 6 deletions core/server/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,24 @@ import (
)

func (cs *coreServer) ListEvents(ctx context.Context, msg *pb.ListEventsRequest) (*pb.ListEventsResponse, error) {
k8s, err := cs.clustersManager.GetImpersonatedClient(ctx, auth.Principal(ctx))
if err != nil {
return nil, doClientError(err)
}

if msg.InvolvedObject == nil {
return nil, status.Errorf(codes.InvalidArgument, "bad request: no object was specified")
}

var clustersClient clustersmngr.Client

var err error

if msg.InvolvedObject.ClusterName != "" {
clustersClient, err = cs.clustersManager.GetImpersonatedClientForCluster(ctx, auth.Principal(ctx), msg.InvolvedObject.ClusterName)
} else {
clustersClient, err = cs.clustersManager.GetImpersonatedClient(ctx, auth.Principal(ctx))
}

if err != nil {
return nil, doClientError(err)
}

clist := clustersmngr.NewClusteredList(func() client.ObjectList {
return &corev1.EventList{}
})
Expand All @@ -42,7 +51,7 @@ func (cs *coreServer) ListEvents(ctx context.Context, msg *pb.ListEventsRequest)
"involvedObject.namespace": msg.InvolvedObject.Namespace,
}

if err := list(ctx, k8s, temporarilyEmptyAppName, msg.InvolvedObject.Namespace, clist, fields); err != nil {
if err := list(ctx, clustersClient, temporarilyEmptyAppName, msg.InvolvedObject.Namespace, clist, fields); err != nil {
return nil, fmt.Errorf("could not get events: %w", err)
}

Expand Down
15 changes: 15 additions & 0 deletions core/server/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ func TestListEvents(t *testing.T) {

g.Expect(res.Events[0].Component).To(Equal(kustomizeEvent.Source.Component))

// Get kustomization events, explicit cluster
res, err = c.ListEvents(ctx, &pb.ListEventsRequest{
InvolvedObject: &pb.ObjectRef{
Name: kustomizationObjectName,
Namespace: ns.Name,
Kind: kustomizev1.KustomizationKind,
ClusterName: "Default",
},
})
g.Expect(err).NotTo(HaveOccurred())

g.Expect(res.Events).To(HaveLen(1))

g.Expect(res.Events[0].Component).To(Equal(kustomizeEvent.Source.Component))

// Get helmrelease events
res, err = c.ListEvents(ctx, &pb.ListEventsRequest{
InvolvedObject: &pb.ObjectRef{
Expand Down
8 changes: 4 additions & 4 deletions core/server/suspend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,20 @@ func TestSuspend_Suspend(t *testing.T) {
},
}

requestObjects := []*api.ClusteredObjRef{}
requestObjects := []*api.ObjectRef{}

for _, tt := range tests {
t.Run(tt.kind, func(t *testing.T) {
g := NewGomegaWithT(t)
g.Expect(k.Create(ctx, tt.obj)).Should(Succeed())
object := &api.ClusteredObjRef{
object := &api.ObjectRef{
Kind: tt.kind,
Name: tt.obj.GetName(),
Namespace: tt.obj.GetNamespace(),
ClusterName: "Default",
}
req := &api.ToggleSuspendResourceRequest{
Objects: []*api.ClusteredObjRef{object},
Objects: []*api.ObjectRef{object},
Suspend: true,
}
_, err = c.ToggleSuspendResource(ctx, req)
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestSuspend_Suspend(t *testing.T) {

_, err = c.ToggleSuspendResource(ctx, &api.ToggleSuspendResourceRequest{

Objects: []*api.ClusteredObjRef{{
Objects: []*api.ObjectRef{{
Kind: sourcev1.GitRepositoryKind,
Name: "fakeName",
Namespace: "fakeNamespace",
Expand Down
10 changes: 5 additions & 5 deletions core/server/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ func TestSync(t *testing.T) {
}{{
name: "kustomization no source",
msg: &pb.SyncFluxObjectRequest{
Objects: []*pb.ClusteredObjRef{{ClusterName: "Default",
Objects: []*pb.ObjectRef{{ClusterName: "Default",
Kind: kustomizev1.KustomizationKind}},
WithSource: false,
},
automation: fluxsync.KustomizationAdapter{Kustomization: kust},
}, {
name: "kustomization with source",
msg: &pb.SyncFluxObjectRequest{
Objects: []*pb.ClusteredObjRef{{ClusterName: "Default",
Objects: []*pb.ObjectRef{{ClusterName: "Default",
Kind: kustomizev1.KustomizationKind}},
WithSource: true,
},
Expand All @@ -78,15 +78,15 @@ func TestSync(t *testing.T) {
}, {
name: "helm release no source",
msg: &pb.SyncFluxObjectRequest{
Objects: []*pb.ClusteredObjRef{{ClusterName: "Default",
Objects: []*pb.ObjectRef{{ClusterName: "Default",
Kind: helmv2.HelmReleaseKind}},
WithSource: false,
},
automation: fluxsync.HelmReleaseAdapter{HelmRelease: hr},
}, {
name: "helm release with source",
msg: &pb.SyncFluxObjectRequest{
Objects: []*pb.ClusteredObjRef{{ClusterName: "Default",
Objects: []*pb.ObjectRef{{ClusterName: "Default",
Kind: helmv2.HelmReleaseKind}},
WithSource: true,
},
Expand All @@ -96,7 +96,7 @@ func TestSync(t *testing.T) {
{
name: "multiple objects",
msg: &pb.SyncFluxObjectRequest{
Objects: []*pb.ClusteredObjRef{{ClusterName: "Default",
Objects: []*pb.ObjectRef{{ClusterName: "Default",
Kind: helmv2.HelmReleaseKind}, {ClusterName: "Default",
Kind: helmv2.HelmReleaseKind}},
WithSource: true,
Expand Down
Loading

0 comments on commit 38e3e8d

Please sign in to comment.