client

package
v0.33.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 16, 2024 License: MPL-2.0 Imports: 17 Imported by: 1

Documentation

Overview

Package client provides Omni API client.

Package client provides Omni API client.

Example
package main

import (
	"context"
	"log"

	"github.com/cosi-project/runtime/pkg/safe"
	"google.golang.org/protobuf/types/known/emptypb"

	"github.com/siderolabs/omni/client/pkg/client"
	"github.com/siderolabs/omni/client/pkg/omni/resources"
	"github.com/siderolabs/omni/client/pkg/omni/resources/omni"
	"github.com/siderolabs/omni/client/pkg/template"
	"github.com/siderolabs/omni/client/pkg/version"
)

func main() {
	// This example shows how to use Omni client to access resources.

	// Setup versions information. You can embed that into `go build` too.
	version.Name = "omni"
	version.SHA = "build SHA"
	version.Tag = "v0.9.1"

	// For this example we will use Omni service account.
	// You can create your service account in advance:
	//
	// omnictl serviceaccount create example.account
	// Created service account "example.account" with public key ID "<REDACTED>"
	//
	// Set the following environment variables to use the service account:
	// OMNI_ENDPOINT=https://<account>.omni.siderolabs.io:443
	// OMNI_SERVICE_ACCOUNT_KEY=base64encodedkey
	//
	// Note: Store the service account key securely, it will not be displayed again

	ctx := context.Background()

	// Creating a new client.
	client, err := client.New(ctx, "https://<account>.omni.siderolabs.io:443", client.WithServiceAccount(
		"base64encodedkey", // From the generated service account.
	))
	if err != nil {
		log.Fatalf("failed to create omni client %s", err)
	}

	// Omni service is using COSI https://github.com/cosi-project/runtime/.
	// The same client is used to get resources in Talos.
	st := client.Omni().State()

	// Getting the resources from the Omni state.
	machines, err := safe.StateList[*omni.MachineStatus](ctx, st, omni.NewMachineStatus(resources.DefaultNamespace, "").Metadata())
	if err != nil {
		log.Fatalf("failed to get machines %s", err)
	}

	var (
		cluster string
		machine *omni.MachineStatus
	)

	for iter := machines.Iterator(); iter.Next(); {
		item := iter.Value()

		log.Printf("machine %s, connected: %t", item.Metadata(), item.TypedSpec().Value.GetConnected())

		// Check cluster assignment for a machine.
		// Find a machine which is allocated into a cluster for the later use.
		if c, ok := item.Metadata().Labels().Get(omni.LabelCluster); ok && machine == nil {
			cluster = c
			machine = item
		}
	}

	// Creating an empty cluster via template.
	// Alternative is to use template.Load to load a cluster template.
	template := template.WithCluster("example.cluster")

	if _, err = template.Sync(ctx, st); err != nil {
		log.Fatalf("failed to sync cluster %s", err)
	}

	log.Printf("sync cluster")

	// Delete cluster.
	if _, err = template.Delete(ctx, st); err != nil {
		log.Fatalf("failed to delete the cluster %s", err)
	}

	log.Printf("destroyed cluster")

	// No machines found, exit.
	if machine == nil {
		log.Printf("no allocated machines found, exit")

		return
	}

	// Using Talos through Omni.
	// Use cluster and machine which we previously found.
	cpuInfo, err := client.Talos().WithCluster(
		cluster,
	).WithNodes(
		machine.Metadata().ID(), // You can use machine UUID as Omni will properly resolve it into machine IP.
	).CPUInfo(ctx, &emptypb.Empty{})
	if err != nil {
		log.Fatalf("failed to read machine CPU info %s", err)
	}

	for _, message := range cpuInfo.Messages {
		for i, info := range message.CpuInfo {
			log.Printf("machine %s, CPU %d family %s", machine.Metadata(), i, info.CpuFamily)
		}

		if len(message.CpuInfo) == 0 {
			log.Printf("no CPU info for machine %s", machine.Metadata())
		}
	}

	// Talking to Omni specific APIs: getting talosconfig.
	_, err = client.Management().Talosconfig(ctx)
	if err != nil {
		log.Fatalf("failed to get talosconfig %s", err)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is Omni API client.

func New

func New(ctx context.Context, endpoint string, opts ...Option) (*Client, error)

New creates a new Omni API client.

func (*Client) Auth

func (c *Client) Auth() *auth.Client

Auth provides access to the auth API.

func (*Client) Close

func (c *Client) Close() error

Close the client.

func (*Client) Endpoint

func (c *Client) Endpoint() string

Endpoint returns the endpoint this client is configured to talk to.

func (*Client) Management

func (c *Client) Management() *management.Client

Management provides access to the management API.

func (*Client) OIDC

func (c *Client) OIDC() *oidc.Client

OIDC provides access to the OIDC API.

func (*Client) Omni

func (c *Client) Omni() *omni.Client

Omni provides access to Omni resource API.

func (*Client) Talos

func (c *Client) Talos() *talos.Client

Talos provides access to Talos machine API.

type Option

type Option func(*Options)

Option is a functional option for the client.

func WithGrpcOpts

func WithGrpcOpts(opts ...grpc.DialOption) Option

WithGrpcOpts adds additional gRPC dial options to the client.

func WithInsecureSkipTLSVerify

func WithInsecureSkipTLSVerify(insecureSkipTLSVerify bool) Option

WithInsecureSkipTLSVerify creates the client with insecure TLS verification.

func WithServiceAccount

func WithServiceAccount(serviceAccountBase64 string) Option

WithServiceAccount creates the client authenticating with the given service account.

func WithUserAccount

func WithUserAccount(contextName, identity string) Option

WithUserAccount is used for accessing Omni by a human.

type Options

type Options struct {
	AuthInterceptor *interceptor.Interceptor

	AdditionalGRPCDialOptions []grpc.DialOption

	InsecureSkipTLSVerify bool
}

Options is the options for the client.

Directories

Path Synopsis
Package management provides client for Omni management API.
Package management provides client for Omni management API.
Package oidc provides client for Omni OIDC API.
Package oidc provides client for Omni OIDC API.
Package omni provides client for Omni resource access.
Package omni provides client for Omni resource access.
Package talos provides helpers for accessing Talos Machine API.
Package talos provides helpers for accessing Talos Machine API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL