|
| 1 | +/* |
| 2 | +Copyright 2022 TriggerMesh Inc. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package describe |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "path" |
| 23 | + |
| 24 | + "github.com/spf13/cobra" |
| 25 | + "github.com/spf13/viper" |
| 26 | + |
| 27 | + "github.com/triggermesh/tmcli/pkg/docker" |
| 28 | + "github.com/triggermesh/tmcli/pkg/manifest" |
| 29 | + "github.com/triggermesh/tmcli/pkg/output" |
| 30 | + "github.com/triggermesh/tmcli/pkg/triggermesh" |
| 31 | + tmbroker "github.com/triggermesh/tmcli/pkg/triggermesh/broker" |
| 32 | + "github.com/triggermesh/tmcli/pkg/triggermesh/source" |
| 33 | + "github.com/triggermesh/tmcli/pkg/triggermesh/target" |
| 34 | + "github.com/triggermesh/tmcli/pkg/triggermesh/transformation" |
| 35 | +) |
| 36 | + |
| 37 | +const manifestFile = "manifest.yaml" |
| 38 | + |
| 39 | +type integration struct { |
| 40 | + Broker component |
| 41 | + Source component |
| 42 | + Transformation component |
| 43 | + Target component |
| 44 | +} |
| 45 | + |
| 46 | +type component struct { |
| 47 | + object []triggermesh.Component |
| 48 | + container []*docker.Container |
| 49 | +} |
| 50 | + |
| 51 | +type DescribeOptions struct { |
| 52 | + ConfigDir string |
| 53 | + CRD string |
| 54 | + Version string |
| 55 | +} |
| 56 | + |
| 57 | +func NewCmd() *cobra.Command { |
| 58 | + o := &DescribeOptions{} |
| 59 | + return &cobra.Command{ |
| 60 | + Use: "describe <broker>", |
| 61 | + Short: "Show broker status", |
| 62 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 63 | + broker := viper.GetString("context") |
| 64 | + if len(args) == 1 { |
| 65 | + broker = args[0] |
| 66 | + } |
| 67 | + configDir, err := cmd.Flags().GetString("config") |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + o.ConfigDir = configDir |
| 72 | + o.Version = viper.GetString("triggermesh.version") |
| 73 | + o.CRD = viper.GetString("triggermesh.servedCRD") |
| 74 | + return o.describe(broker) |
| 75 | + }, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func (o DescribeOptions) describe(broker string) error { |
| 80 | + ctx := context.Background() |
| 81 | + configDir := path.Join(o.ConfigDir, broker) |
| 82 | + manifestFile := path.Join(configDir, manifestFile) |
| 83 | + manifest := manifest.New(manifestFile) |
| 84 | + if err := manifest.Read(); err != nil { |
| 85 | + return fmt.Errorf("cannot parse manifest: %w", err) |
| 86 | + } |
| 87 | + |
| 88 | + var intg integration |
| 89 | + for _, object := range manifest.Objects { |
| 90 | + switch { |
| 91 | + case object.Kind == "Broker": |
| 92 | + co, err := tmbroker.New(object.Metadata.Name, configDir) |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("creating broker object: %v", err) |
| 95 | + } |
| 96 | + cc, err := triggermesh.Info(ctx, co) |
| 97 | + if err != nil { |
| 98 | + // ignore the error |
| 99 | + cc = nil |
| 100 | + } |
| 101 | + intg.Broker = component{ |
| 102 | + object: []triggermesh.Component{co}, |
| 103 | + container: []*docker.Container{cc}, |
| 104 | + } |
| 105 | + case object.Kind == "Transformation": |
| 106 | + co := transformation.New(object.Metadata.Name, o.CRD, object.Kind, broker, o.Version, object.Spec) |
| 107 | + cc, err := triggermesh.Info(ctx, co) |
| 108 | + if err != nil { |
| 109 | + // ignore the error |
| 110 | + cc = nil |
| 111 | + } |
| 112 | + intg.Transformation.object = append(intg.Transformation.object, co) |
| 113 | + intg.Transformation.container = append(intg.Transformation.container, cc) |
| 114 | + case object.APIVersion == "sources.triggermesh.io/v1alpha1": |
| 115 | + co := source.New(object.Metadata.Name, o.CRD, object.Kind, broker, o.Version, object.Spec) |
| 116 | + cc, err := triggermesh.Info(ctx, co) |
| 117 | + if err != nil { |
| 118 | + // ignore the error |
| 119 | + cc = nil |
| 120 | + } |
| 121 | + intg.Source.object = append(intg.Source.object, co) |
| 122 | + intg.Source.container = append(intg.Source.container, cc) |
| 123 | + case object.APIVersion == "targets.triggermesh.io/v1alpha1": |
| 124 | + co := target.New(object.Metadata.Name, o.CRD, object.Kind, broker, o.Version, object.Spec) |
| 125 | + cc, err := triggermesh.Info(ctx, co) |
| 126 | + if err != nil { |
| 127 | + // ignore the error |
| 128 | + cc = nil |
| 129 | + } |
| 130 | + intg.Target.object = append(intg.Target.object, co) |
| 131 | + intg.Target.container = append(intg.Target.container, cc) |
| 132 | + default: |
| 133 | + continue |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + output.DescribeBroker(intg.Broker.object, intg.Broker.container) |
| 138 | + output.DescribeSource(intg.Source.object, intg.Source.container) |
| 139 | + output.DescribeTransformation(intg.Transformation.object, intg.Transformation.container) |
| 140 | + output.DescribeTarget(intg.Target.object, intg.Target.container) |
| 141 | + |
| 142 | + return nil |
| 143 | +} |
0 commit comments