Skip to content

Commit

Permalink
build: update kc sdk with merged byUUID/byName methods (#1457)
Browse files Browse the repository at this point in the history
Reviewed-by: Cezar Craciunoiu <[email protected]>
Approved-by: Cezar Craciunoiu <[email protected]>
  • Loading branch information
nderjung authored Mar 26, 2024
2 parents ba7d230 + 68d68aa commit 583f2d7
Show file tree
Hide file tree
Showing 29 changed files with 96 additions and 326 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ require (
k8s.io/apimachinery v0.29.2
k8s.io/apiserver v0.29.2
oras.land/oras-go/v2 v2.4.0
sdk.kraft.cloud v0.5.5-0.20240324162937-97e7a02629be
sdk.kraft.cloud v0.5.5-0.20240326115105-3db0329eacfe
sigs.k8s.io/kustomize/kyaml v0.14.3
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1673,8 +1673,8 @@ oras.land/oras-go/v2 v2.4.0/go.mod h1:osvtg0/ClRq1KkydMAEu/IxFieyjItcsQ4ut4PPF+f
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sdk.kraft.cloud v0.5.5-0.20240324162937-97e7a02629be h1:6Pvj5Ko1vhJNQhkUZ0ZBLkS43t1CWdlNpsvi70SVboI=
sdk.kraft.cloud v0.5.5-0.20240324162937-97e7a02629be/go.mod h1:lgGLp9jSBwSdeQkwrcQZHuejQbkgE3AqbIN+mnaKDkg=
sdk.kraft.cloud v0.5.5-0.20240326115105-3db0329eacfe h1:B1+Hz3PJGMusV1TI0Imeb/XIKcpqSd4wlrzQWbQlPX8=
sdk.kraft.cloud v0.5.5-0.20240326115105-3db0329eacfe/go.mod h1:nan+XwhrD9FC/gntnkVGg+n55q/ExKUwrUhAh+xor+g=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.14/go.mod h1:LEScyzhFmoF5pso/YSeBstl57mOzx9xlU9n85RGrDQg=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.15/go.mod h1:LEScyzhFmoF5pso/YSeBstl57mOzx9xlU9n85RGrDQg=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.28.0 h1:TgtAeesdhpm2SGwkQasmbeqDo8th5wOBA5h/AjTKA4I=
Expand Down
9 changes: 1 addition & 8 deletions internal/cli/kraft/cloud/certificate/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/spf13/cobra"

kraftcloud "sdk.kraft.cloud"
kccerts "sdk.kraft.cloud/certificates"
kcclient "sdk.kraft.cloud/client"

"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
Expand Down Expand Up @@ -87,12 +85,7 @@ func (opts *GetOptions) Run(ctx context.Context, args []string) error {
kraftcloud.WithToken(config.GetKraftCloudTokenAuthConfig(*auth)),
)

var certResp *kcclient.ServiceResponse[kccerts.GetResponseItem]
if utils.IsUUID(args[0]) {
certResp, err = client.WithMetro(opts.metro).GetByUUIDs(ctx, args[0])
} else {
certResp, err = client.WithMetro(opts.metro).GetByNames(ctx, args[0])
}
certResp, err := client.WithMetro(opts.metro).Get(ctx, args[0])
if err != nil {
return fmt.Errorf("could not get certificate %s: %w", args[0], err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/kraft/cloud/certificate/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (opts *ListOptions) Run(ctx context.Context, args []string) error {
for _, certItem := range certList {
uuids = append(uuids, certItem.UUID)
}
certsResp, err := client.WithMetro(opts.metro).GetByUUIDs(ctx, uuids...)
certsResp, err := client.WithMetro(opts.metro).Get(ctx, uuids...)
if err != nil {
return fmt.Errorf("getting details of %d certificate(s): %w", len(certList), err)
}
Expand Down
51 changes: 8 additions & 43 deletions internal/cli/kraft/cloud/certificate/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/spf13/cobra"

kraftcloud "sdk.kraft.cloud"
kccerts "sdk.kraft.cloud/certificates"
kcclient "sdk.kraft.cloud/client"

"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
Expand Down Expand Up @@ -99,8 +97,6 @@ func (opts *RemoveOptions) Run(ctx context.Context, args []string) error {
kraftcloud.WithToken(config.GetKraftCloudTokenAuthConfig(*auth)),
)

var delResp *kcclient.ServiceResponse[kccerts.DeleteResponseItem]

if opts.All {
certListResp, err := client.WithMetro(opts.metro).List(ctx)
if err != nil {
Expand All @@ -121,55 +117,24 @@ func (opts *RemoveOptions) Run(ctx context.Context, args []string) error {
uuids = append(uuids, certItem.UUID)
}

if delResp, err = client.WithMetro(opts.metro).DeleteByUUIDs(ctx, uuids...); err != nil {
return fmt.Errorf("removing %d certificate(s): %w", len(certList), err)
delResp, err := client.WithMetro(opts.metro).Delete(ctx, uuids...)
if err != nil {
return fmt.Errorf("removing %d certificate(s): %w", len(uuids), err)
}
if _, err = delResp.AllOrErr(); err != nil {
return fmt.Errorf("removing %d certificate(s): %w", len(certList), err)
return fmt.Errorf("removing %d certificate(s): %w", len(uuids), err)
}
return nil
}

log.G(ctx).Infof("Removing %d certificate(s)", len(args))

allUUIDs := true
allNames := true
for _, arg := range args {
if utils.IsUUID(arg) {
allNames = false
} else {
allUUIDs = false
}
if !(allUUIDs || allNames) {
break
}
}

switch {
case allUUIDs:
if delResp, err = client.WithMetro(opts.metro).DeleteByUUIDs(ctx, args...); err != nil {
return fmt.Errorf("removing %d certificate(s): %w", len(args), err)
}
case allNames:
if delResp, err = client.WithMetro(opts.metro).DeleteByNames(ctx, args...); err != nil {
return fmt.Errorf("removing %d certificate(s): %w", len(args), err)
}
default:
for _, arg := range args {
log.G(ctx).Infof("Removing certificate %s", arg)

if utils.IsUUID(arg) {
delResp, err = client.WithMetro(opts.metro).DeleteByUUIDs(ctx, arg)
} else {
delResp, err = client.WithMetro(opts.metro).DeleteByNames(ctx, arg)
}
if err != nil {
return fmt.Errorf("could not remove certificate %s: %w", arg, err)
}
}
delResp, err := client.WithMetro(opts.metro).Delete(ctx, args...)
if err != nil {
return fmt.Errorf("removing %d certificate(s): %w", len(args), err)
}
if _, err = delResp.AllOrErr(); err != nil {
return fmt.Errorf("removing certificate(s): %w", err)
return fmt.Errorf("removing %d certificate(s): %w", len(args), err)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/kraft/cloud/compose/down/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (opts *UpOptions) Run(ctx context.Context, args []string) error {
if service.Name != opts.project.Name+"-"+requestedService {
continue
}
instanceResp, err := opts.Client.WithMetro(opts.Metro).GetByNames(ctx, service.Name)
instanceResp, err := opts.Client.WithMetro(opts.Metro).Get(ctx, service.Name)
if err != nil {
return fmt.Errorf("getting instance %s: %w", service.Name, err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/kraft/cloud/compose/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (opts *UpOptions) Run(ctx context.Context, args []string) error {
}
log.G(ctx).WithField("service", service.Name).Info("starting service")

instanceResp, err := opts.Client.WithMetro(opts.Metro).GetByNames(ctx, service.Name)
instanceResp, err := opts.Client.WithMetro(opts.Metro).Get(ctx, service.Name)
if err != nil {
return fmt.Errorf("getting instance %s: %w", service.Name, err)
}
Expand Down
5 changes: 2 additions & 3 deletions internal/cli/kraft/cloud/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,11 @@ func (opts *DeployOptions) Run(ctx context.Context, args []string) error {

// Preflight check: check if `--name` is already taken:
if len(opts.Name) > 0 {
resp, err := opts.Client.Instances().WithMetro(opts.Metro).GetByNames(ctx, opts.Name)
resp, err := opts.Client.Instances().WithMetro(opts.Metro).Get(ctx, opts.Name)
if err != nil {
return err
}
_, err = resp.AllOrErr()
if err == nil {
if _, err = resp.AllOrErr(); err == nil {
return fmt.Errorf("instance name '%s' is already taken", opts.Name)
}
}
Expand Down
33 changes: 11 additions & 22 deletions internal/cli/kraft/cloud/instance/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,7 @@ func Create(ctx context.Context, opts *CreateOptions, args ...string) (*kcclient
var serviceGroup *kcservices.GetResponseItem

if opts.ServiceGroupNameOrUUID != "" {
var resp *kcclient.ServiceResponse[kcservices.GetResponseItem]
if utils.IsUUID(opts.ServiceGroupNameOrUUID) {
resp, err = opts.Client.Services().WithMetro(opts.Metro).GetByUUIDs(ctx, opts.ServiceGroupNameOrUUID)
} else {
resp, err = opts.Client.Services().WithMetro(opts.Metro).GetByNames(ctx, opts.ServiceGroupNameOrUUID)
}
resp, err := opts.Client.Services().WithMetro(opts.Metro).Get(ctx, opts.ServiceGroupNameOrUUID)
if err != nil {
return nil, nil, fmt.Errorf("could not use service %s: %w", opts.ServiceGroupNameOrUUID, err)
}
Expand Down Expand Up @@ -369,7 +364,7 @@ func Create(ctx context.Context, opts *CreateOptions, args ...string) (*kcclient
return nil, nil, err
}

instanceResp, err := opts.Client.Instances().WithMetro(opts.Metro).GetByUUIDs(ctx, newInstance.UUID)
instanceResp, err := opts.Client.Instances().WithMetro(opts.Metro).Get(ctx, newInstance.UUID)
if err != nil {
return nil, nil, fmt.Errorf("getting details of instance %s: %w", newInstance.UUID, err)
}
Expand All @@ -379,7 +374,7 @@ func Create(ctx context.Context, opts *CreateOptions, args ...string) (*kcclient
}

if sg := instance.ServiceGroup; sg != nil && sg.UUID != "" {
serviceGroupResp, err := opts.Client.Services().WithMetro(opts.Metro).GetByUUIDs(ctx, sg.UUID)
serviceGroupResp, err := opts.Client.Services().WithMetro(opts.Metro).Get(ctx, sg.UUID)
if err != nil {
return nil, nil, fmt.Errorf("getting details of service %s: %w", sg.UUID, err)
}
Expand All @@ -393,8 +388,9 @@ func Create(ctx context.Context, opts *CreateOptions, args ...string) (*kcclient

// Rollout an instance in a service group.
func Rollout(ctx context.Context, opts *CreateOptions, newInstance *kcinstances.GetResponseItem, newServiceGroup *kcservices.GetResponseItem) error {
_, err := opts.Client.Instances().WithMetro(opts.Metro).
WaitByUUIDs(ctx, kcinstances.StateRunning, int(time.Minute.Milliseconds()), newInstance.UUID)
oneMinute := int(time.Minute.Milliseconds())

_, err := opts.Client.Instances().WithMetro(opts.Metro).Wait(ctx, kcinstances.StateRunning, oneMinute, newInstance.UUID)
if err != nil {
return fmt.Errorf("could not wait for the first new instance to start: %w", err)
}
Expand All @@ -414,14 +410,11 @@ func Rollout(ctx context.Context, opts *CreateOptions, newInstance *kcinstances.
WithField("service group", newServiceGroup.Name).
Info("draining old instance")

if _, err := opts.Client.Instances().WithMetro(opts.Metro).
StopByUUIDs(ctx, int(time.Minute.Milliseconds()), false, instance.UUID); err != nil {
if _, err = opts.Client.Instances().WithMetro(opts.Metro).Stop(ctx, oneMinute, false, instance.UUID); err != nil {
return fmt.Errorf("could not stop the old instance: %w", err)
}

_, err = opts.Client.Instances().WithMetro(opts.Metro).
WaitByUUIDs(ctx, kcinstances.StateStopped, int(time.Minute.Milliseconds()), instance.UUID)
if err != nil {
if _, err = opts.Client.Instances().WithMetro(opts.Metro).Wait(ctx, kcinstances.StateStopped, oneMinute, instance.UUID); err != nil {
return fmt.Errorf("could not wait for the old instance to stop: %w", err)
}

Expand Down Expand Up @@ -461,8 +454,7 @@ func Rollout(ctx context.Context, opts *CreateOptions, newInstance *kcinstances.
req.MemoryMB = &opts.Memory
}

_, err := opts.Client.Instances().WithMetro(opts.Metro).Create(ctx, req)
if err != nil {
if _, err := opts.Client.Instances().WithMetro(opts.Metro).Create(ctx, req); err != nil {
return fmt.Errorf("could not create a new instance: %w", err)
}

Expand All @@ -471,14 +463,11 @@ func Rollout(ctx context.Context, opts *CreateOptions, newInstance *kcinstances.
WithField("service group", newServiceGroup.Name).
Info("draining old instance")

if _, err := opts.Client.Instances().WithMetro(opts.Metro).
StopByUUIDs(ctx, int(time.Minute.Milliseconds()), false, instance.UUID); err != nil {
if _, err := opts.Client.Instances().WithMetro(opts.Metro).Stop(ctx, oneMinute, false, instance.UUID); err != nil {
return fmt.Errorf("could not stop the old instance: %w", err)
}

_, err = opts.Client.Instances().WithMetro(opts.Metro).
WaitByUUIDs(ctx, kcinstances.StateStopped, int(time.Minute.Milliseconds()), instance.UUID)
if err != nil {
if _, err = opts.Client.Instances().WithMetro(opts.Metro).Wait(ctx, kcinstances.StateStopped, oneMinute, instance.UUID); err != nil {
return fmt.Errorf("could not wait for the old instance to stop: %w", err)
}

Expand Down
9 changes: 1 addition & 8 deletions internal/cli/kraft/cloud/instance/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/spf13/cobra"

kraftcloud "sdk.kraft.cloud"
kcclient "sdk.kraft.cloud/client"
kcinstances "sdk.kraft.cloud/instances"

"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
Expand Down Expand Up @@ -87,12 +85,7 @@ func (opts *GetOptions) Run(ctx context.Context, args []string) error {
kraftcloud.WithToken(config.GetKraftCloudTokenAuthConfig(*auth)),
)

var instanceResp *kcclient.ServiceResponse[kcinstances.GetResponseItem]
if utils.IsUUID(args[0]) {
instanceResp, err = client.WithMetro(opts.metro).GetByUUIDs(ctx, args[0])
} else {
instanceResp, err = client.WithMetro(opts.metro).GetByNames(ctx, args[0])
}
instanceResp, err := client.WithMetro(opts.metro).Get(ctx, args[0])
if err != nil {
return fmt.Errorf("could not get instance %s: %w", args[0], err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/kraft/cloud/instance/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (opts *ListOptions) Run(ctx context.Context, args []string) error {
uuids = append(uuids, instItem.UUID)
}

instancesResp, err := client.WithMetro(opts.metro).GetByUUIDs(ctx, uuids...)
instancesResp, err := client.WithMetro(opts.metro).Get(ctx, uuids...)
if err != nil {
return fmt.Errorf("getting details of %d instance(s): %w", len(instList), err)
}
Expand Down
10 changes: 1 addition & 9 deletions internal/cli/kraft/cloud/instance/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/spf13/cobra"

kraftcloud "sdk.kraft.cloud"
kcclient "sdk.kraft.cloud/client"
kcinstances "sdk.kraft.cloud/instances"

"kraftkit.sh/cmdfactory"
Expand Down Expand Up @@ -103,14 +102,7 @@ func (opts *LogOptions) Pre(cmd *cobra.Command, _ []string) error {
}

func (opts *LogOptions) logsFetchDecode(ctx context.Context, client kcinstances.InstancesService, image string, offset, limit int) ([]byte, *kcinstances.LogResponseItem, error) {
var err error

var logResp *kcclient.ServiceResponse[kcinstances.LogResponseItem]
if utils.IsUUID(image) {
logResp, err = client.WithMetro(opts.metro).LogByUUID(ctx, image, offset, limit)
} else {
logResp, err = client.WithMetro(opts.metro).LogByName(ctx, image, offset, limit)
}
logResp, err := client.WithMetro(opts.metro).Log(ctx, image, offset, limit)
if err != nil {
return nil, nil, fmt.Errorf("could not retrieve logs: %w", err)
}
Expand Down
48 changes: 7 additions & 41 deletions internal/cli/kraft/cloud/instance/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/spf13/cobra"

kraftcloud "sdk.kraft.cloud"
kcclient "sdk.kraft.cloud/client"
kcinstances "sdk.kraft.cloud/instances"

"kraftkit.sh/cmdfactory"
Expand Down Expand Up @@ -112,8 +111,6 @@ func (opts *RemoveOptions) Run(ctx context.Context, args []string) error {
kraftcloud.WithToken(config.GetKraftCloudTokenAuthConfig(*auth)),
)

var delResp *kcclient.ServiceResponse[kcinstances.DeleteResponseItem]

if opts.All || opts.Stopped {
instListResp, err := client.WithMetro(opts.metro).List(ctx)
if err != nil {
Expand All @@ -133,7 +130,7 @@ func (opts *RemoveOptions) Run(ctx context.Context, args []string) error {
}

if opts.Stopped {
instInfosResp, err := client.WithMetro(opts.metro).GetByUUIDs(ctx, uuids...)
instInfosResp, err := client.WithMetro(opts.metro).Get(ctx, uuids...)
if err != nil {
return fmt.Errorf("could not get instances: %w", err)
}
Expand All @@ -157,7 +154,8 @@ func (opts *RemoveOptions) Run(ctx context.Context, args []string) error {

log.G(ctx).Infof("Removing %d instance(s)", len(uuids))

if delResp, err = client.WithMetro(opts.metro).DeleteByUUIDs(ctx, uuids...); err != nil {
delResp, err := client.WithMetro(opts.metro).Delete(ctx, uuids...)
if err != nil {
return fmt.Errorf("removing %d instance(s): %w", len(uuids), err)
}
if _, err = delResp.AllOrErr(); err != nil {
Expand All @@ -168,44 +166,12 @@ func (opts *RemoveOptions) Run(ctx context.Context, args []string) error {

log.G(ctx).Infof("Removing %d instance(s)", len(args))

allUUIDs := true
allNames := true
for _, arg := range args {
if utils.IsUUID(arg) {
allNames = false
} else {
allUUIDs = false
}
if !(allUUIDs || allNames) {
break
}
}

switch {
case allUUIDs:
if delResp, err = client.WithMetro(opts.metro).DeleteByUUIDs(ctx, args...); err != nil {
return fmt.Errorf("removing %d instance(s): %w", len(args), err)
}
case allNames:
if delResp, err = client.WithMetro(opts.metro).DeleteByNames(ctx, args...); err != nil {
return fmt.Errorf("removing %d instance(s): %w", len(args), err)
}
default:
for _, arg := range args {
log.G(ctx).Infof("Removing instance %s", arg)

if utils.IsUUID(arg) {
delResp, err = client.WithMetro(opts.metro).DeleteByUUIDs(ctx, arg)
} else {
delResp, err = client.WithMetro(opts.metro).DeleteByNames(ctx, arg)
}
if err != nil {
return fmt.Errorf("could not remove instance %s: %w", arg, err)
}
}
delResp, err := client.WithMetro(opts.metro).Delete(ctx, args...)
if err != nil {
return fmt.Errorf("removing %d instance(s): %w", len(args), err)
}
if _, err = delResp.AllOrErr(); err != nil {
return fmt.Errorf("removing instances(s): %w", err)
return fmt.Errorf("removing %d instances(s): %w", len(args), err)
}

return nil
Expand Down
Loading

0 comments on commit 583f2d7

Please sign in to comment.