Skip to content

Commit

Permalink
feat: pull state Artifact
Browse files Browse the repository at this point in the history
Signed-off-by: bupd <[email protected]>
  • Loading branch information
bupd committed Aug 24, 2024
1 parent 6cd1820 commit eb88ffa
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ require (
require (
github.com/rs/zerolog v1.33.0
github.com/spf13/viper v1.19.0
go.uber.org/zap v1.27.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
github.com/stretchr/testify v1.9.0
)

require (
Expand Down Expand Up @@ -396,6 +395,7 @@ require (
go.opentelemetry.io/otel/trace v1.27.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
golang.org/x/mod v0.17.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2163,8 +2163,6 @@ gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/resty.v1 v1.12.0 h1:CuXP0Pjfw9rOuY6EP+UvtNvt5DSqHpIxILZKT/quCZI=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/square/go-jose.v2 v2.6.0 h1:NGk74WTnPKBNUhNzQX7PYcTLUjoq7mzKk2OKbvwk2iI=
Expand Down
5 changes: 5 additions & 0 deletions internal/satellite/satellite.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"container-registry.com/harbor-satellite/internal/replicate"
"container-registry.com/harbor-satellite/internal/state"
"container-registry.com/harbor-satellite/internal/store"
"container-registry.com/harbor-satellite/logger"
)
Expand All @@ -24,6 +25,10 @@ func NewSatellite(ctx context.Context, storer store.Storer, replicator replicate
func (s *Satellite) Run(ctx context.Context) error {
log := logger.FromContext(ctx)

if err := state.PullStateArtifact(ctx, "demo.goharbor.io", "satellite-01", "latest"); err != nil {
log.Error().Msgf("error in getting state artifact: %v", err)
}

// Execute the initial operation immediately without waiting for the ticker
imgs, err := s.storer.List(ctx)
if err != nil {
Expand Down
48 changes: 48 additions & 0 deletions internal/state/stateArtifact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package state

import (
"context"
"fmt"
"os"

"container-registry.com/harbor-satellite/internal/utils"
"container-registry.com/harbor-satellite/logger"
"github.com/google/go-containerregistry/pkg/crane"
)

// pulls State Artifact from given registry.
func PullStateArtifact(ctx context.Context, reg, groupName, tag string) error {
log := logger.FromContext(ctx)
// TO-DO: Implementation of reg groupName tag will be handled in ztr
// imageRef := "demo.goharbor.io/satellite/satellite-01:latest"
imageRef := fmt.Sprintf("%s/satellite/%s:%s", reg, groupName, tag)

auth, err := utils.Auth()
if err != nil {
return fmt.Errorf("error in authentication: %v", err)
}

log.Info().Msg("Pulling State Artifact")

// Pull the image with authentication
img, err := crane.Pull(imageRef, crane.WithAuth(auth), crane.Insecure)
if err != nil {
// panic(fmt.Sprintf("Failed to pull image: %v", err))
return fmt.Errorf("failed to pull state artifact: %v", err)
}

out, err := os.Create("state.yaml")
if err != nil {
return fmt.Errorf("failed to create state.yml file: %v", err)
}
defer out.Close()

// export image
if err := crane.Export(img, out); err != nil {
return fmt.Errorf("failed to write state.yml file: %v", err)
}

log.Info().Msgf("successfully written state to: %s", out.Name())

return nil
}

0 comments on commit eb88ffa

Please sign in to comment.