This repository was archived by the owner on Sep 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
OSAC-1346: add public gRPC servers and server wiring for bare metal instances #707
Merged
openshift-merge-bot
merged 5 commits into
osac-project:main
from
adriengentil:feat/OSAC-1343
Jun 19, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fc572fb
OSAC-1346: add public gRPC servers and server wiring for bare metal i…
adriengentil de6899a
OSAC-1349: add OPA policies for bare metal instance resources
adriengentil 4bc9c44
OSAC-1350: add CLI commands for bare metal instances
adriengentil bca99eb
OSAC-1346: add rendering table definitions for bare metal instance re…
adriengentil 0d0af6b
OSAC-1346: add HUB column to private BareMetalInstance table
adriengentil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
internal/cmd/cli/create/baremetalinstance/create_bare_metal_instance_cmd.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| /* | ||
| Copyright (c) 2025 Red Hat 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 baremetalinstance | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log/slog" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "google.golang.org/protobuf/proto" | ||
|
|
||
| publicv1 "github.com/osac-project/fulfillment-service/internal/api/osac/public/v1" | ||
| "github.com/osac-project/fulfillment-service/internal/config" | ||
| "github.com/osac-project/fulfillment-service/internal/logging" | ||
| "github.com/osac-project/fulfillment-service/internal/reflection" | ||
| "github.com/osac-project/fulfillment-service/internal/terminal" | ||
| ) | ||
|
|
||
| func Cmd() *cobra.Command { | ||
| runner := &runnerContext{} | ||
| result := &cobra.Command{ | ||
| Use: "baremetalinstance [FLAG...]", | ||
| Aliases: []string{string(proto.MessageName((*publicv1.BareMetalInstance)(nil)))}, | ||
| Short: shortHelp, | ||
| Long: longHelp, | ||
| DisableFlagsInUseLine: true, | ||
| Args: cobra.NoArgs, | ||
| RunE: runner.run, | ||
| } | ||
| flags := result.Flags() | ||
| flags.StringVarP( | ||
| &runner.args.name, | ||
| "name", | ||
| "n", | ||
| "", | ||
| nameFlagHelp, | ||
| ) | ||
| flags.StringVar( | ||
| &runner.args.catalogItem, | ||
| "catalog-item", | ||
| "", | ||
| catalogItemFlagHelp, | ||
| ) | ||
| flags.StringVar( | ||
| &runner.args.sshKey, | ||
| "ssh-key", | ||
| "", | ||
| sshKeyFlagHelp, | ||
| ) | ||
| flags.StringVar( | ||
| &runner.args.userData, | ||
| "user-data", | ||
| "", | ||
| userDataFlagHelp, | ||
| ) | ||
| flags.StringVar( | ||
| &runner.args.runStrategy, | ||
| "run-strategy", | ||
| "", | ||
| runStrategyFlagHelp, | ||
| ) | ||
|
|
||
| if err := result.MarkFlagRequired("catalog-item"); err != nil { | ||
| panic(fmt.Sprintf("failed to mark catalog-item flag as required: %v", err)) | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| type runnerContext struct { | ||
| args struct { | ||
| name string | ||
| catalogItem string | ||
| sshKey string | ||
| userData string | ||
| runStrategy string | ||
| } | ||
| logger *slog.Logger | ||
| } | ||
|
|
||
| func (c *runnerContext) run(cmd *cobra.Command, _ []string) error { | ||
| ctx := cmd.Context() | ||
| c.logger = logging.LoggerFromContext(ctx) | ||
| console := terminal.ConsoleFromContext(ctx) | ||
|
|
||
| cfg := config.SettingsFromContext(ctx) | ||
| if !cfg.Armed() { | ||
| return fmt.Errorf("there is no configuration, run the 'login' command") | ||
| } | ||
|
|
||
| conn, err := cfg.Connect(ctx, cmd.Flags()) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create gRPC connection: %w", err) | ||
| } | ||
| defer conn.Close() | ||
|
|
||
| helper, err := reflection.NewHelper(). | ||
| SetLogger(c.logger). | ||
| SetConnection(conn). | ||
| AddPackages(cfg.Packages()). | ||
| Build() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create reflection tool: %w", err) | ||
| } | ||
| console.SetHelper(helper) | ||
|
|
||
| spec := publicv1.BareMetalInstanceSpec_builder{ | ||
| CatalogItem: c.args.catalogItem, | ||
| } | ||
| if c.args.sshKey != "" { | ||
| sshKey := c.args.sshKey | ||
| spec.SshKey = &sshKey | ||
| } | ||
| if c.args.userData != "" { | ||
| userData := c.args.userData | ||
| spec.UserData = &userData | ||
| } | ||
| if c.args.runStrategy != "" { | ||
| val, ok := publicv1.BareMetalInstanceRunStrategy_value["BARE_METAL_INSTANCE_RUN_STRATEGY_"+strings.ToUpper(c.args.runStrategy)] | ||
| if !ok { | ||
| return fmt.Errorf( | ||
| "unknown run strategy %q, valid values are Always and Halted", | ||
| c.args.runStrategy, | ||
| ) | ||
| } | ||
| rs := publicv1.BareMetalInstanceRunStrategy(val) | ||
| spec.RunStrategy = &rs | ||
| } | ||
|
|
||
| bmi := publicv1.BareMetalInstance_builder{ | ||
| Metadata: publicv1.Metadata_builder{ | ||
| Name: c.args.name, | ||
| }.Build(), | ||
| Spec: spec.Build(), | ||
| }.Build() | ||
|
|
||
| client := publicv1.NewBareMetalInstancesClient(conn) | ||
| response, err := client.Create(ctx, publicv1.BareMetalInstancesCreateRequest_builder{ | ||
| Object: bmi, | ||
| }.Build()) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create bare metal instance: %w", err) | ||
| } | ||
|
|
||
| console.Infof(ctx, "Created bare metal instance '%s'.\n", response.GetObject().GetId()) | ||
| return nil | ||
| } | ||
|
|
||
| const shortHelp = `Create a bare metal instance.` | ||
|
|
||
| const longHelp = ` | ||
| Create a bare metal instance. | ||
| ` | ||
|
|
||
| const nameFlagHelp = ` | ||
| _NAME_ - Name of the bare metal instance. | ||
| ` | ||
|
|
||
| const catalogItemFlagHelp = ` | ||
| _ID_ - Catalog item identifier or name. Required. | ||
| ` | ||
|
|
||
| const sshKeyFlagHelp = ` | ||
| _KEY_ - SSH public key injected into the OS at provisioning time. Must be a | ||
| valid OpenSSH public key. Immutable after creation. | ||
| ` | ||
|
|
||
| const userDataFlagHelp = ` | ||
| _DATA_ - User data passed to the OS at first boot (e.g. cloud-init). | ||
| Maximum 64 KB. Immutable after creation. | ||
| ` | ||
|
|
||
| const runStrategyFlagHelp = ` | ||
| _STRATEGY_ - Run strategy controlling the power state. Valid values are | ||
| {{ bt }}Always{{ bt }} (keep powered on) and {{ bt }}Halted{{ bt }} | ||
| (power off). | ||
| ` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
internal/cmd/cli/describe/baremetalinstance/describe_baremetalinstance_cmd.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| Copyright (c) 2025 Red Hat 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 baremetalinstance | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "strings" | ||
| "text/tabwriter" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "google.golang.org/protobuf/proto" | ||
|
|
||
| publicv1 "github.com/osac-project/fulfillment-service/internal/api/osac/public/v1" | ||
| "github.com/osac-project/fulfillment-service/internal/cmd/cli/lookup" | ||
| "github.com/osac-project/fulfillment-service/internal/config" | ||
| "github.com/osac-project/fulfillment-service/internal/terminal" | ||
| ) | ||
|
|
||
| func Cmd() *cobra.Command { | ||
| result := &cobra.Command{ | ||
| Use: "baremetalinstance [FLAG...] ID|NAME", | ||
| Aliases: []string{"baremetalinstances"}, | ||
| Short: shortHelp, | ||
| Long: longHelp, | ||
| DisableFlagsInUseLine: true, | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: run, | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| func run(cmd *cobra.Command, args []string) error { | ||
| ref := args[0] | ||
| ctx := cmd.Context() | ||
| console := terminal.ConsoleFromContext(ctx) | ||
|
|
||
| cfg := config.SettingsFromContext(ctx) | ||
| if !cfg.Armed() { | ||
| return fmt.Errorf("there is no configuration, run the 'login' command") | ||
| } | ||
|
|
||
| conn, err := cfg.Connect(ctx, cmd.Flags()) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create gRPC connection: %w", err) | ||
| } | ||
| defer conn.Close() | ||
|
|
||
| client := publicv1.NewBareMetalInstancesClient(conn) | ||
|
|
||
| matched, err := lookup.Find(ref, "bare metal instance", func(filter string, limit int32) ([]*publicv1.BareMetalInstance, error) { | ||
| resp, err := client.List(ctx, publicv1.BareMetalInstancesListRequest_builder{ | ||
| Filter: proto.String(filter), | ||
| Limit: proto.Int32(limit), | ||
| }.Build()) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to describe bare metal instance: %w", err) | ||
| } | ||
| return resp.GetItems(), nil | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| renderBareMetalInstance(console, matched) | ||
| return nil | ||
| } | ||
|
|
||
| func renderBareMetalInstance(w io.Writer, bmi *publicv1.BareMetalInstance) { | ||
| writer := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0) | ||
| catalogItem := "-" | ||
| if bmi.Spec != nil { | ||
| if id := bmi.Spec.GetCatalogItem(); id != "" { | ||
| catalogItem = id | ||
| } | ||
| } | ||
| state := "-" | ||
| if bmi.Status != nil { | ||
| state = bmi.Status.State.String() | ||
| state = strings.TrimPrefix(state, "BARE_METAL_INSTANCE_STATE_") | ||
| } | ||
| fmt.Fprintf(writer, "ID:\t%s\n", bmi.Id) | ||
| fmt.Fprintf(writer, "Catalog Item:\t%s\n", catalogItem) | ||
| fmt.Fprintf(writer, "State:\t%s\n", state) | ||
| writer.Flush() | ||
| } | ||
|
|
||
| const shortHelp = `Describe a bare metal instance.` | ||
|
|
||
| const longHelp = ` | ||
| Describe a bare metal instance. | ||
| ` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.