Skip to content

Commit

Permalink
Update to move logic to executor
Browse files Browse the repository at this point in the history
  • Loading branch information
lmzuccarelli committed Sep 17, 2024
1 parent 9facfe3 commit 783ff4b
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 38 deletions.
9 changes: 9 additions & 0 deletions v2/internal/pkg/cli/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,15 @@ func (o *ExecutorSchema) RunMirrorToDisk(cmd *cobra.Command, args []string) erro
copiedSchema = cs
}

// as the signatures are extracted for release in the mirror-to-disk workflow
// we create the configmap here to be included in the tar.
// this avoids adding this logic in both mirror-to-mirror and disk-to-mirror flows
err = o.ClusterResources.GenerateSignatureConfigMap()
if err != nil {
// as this is not a seriously fatal error we just log the error
o.Log.Warn("%s", err)
}

// prepare tar.gz when mirror to disk
// first stop the registry
interruptSig := NormalStorageInterruptErrorf("end of mirroring to disk. Stopping local storage to prepare the archive")
Expand Down
5 changes: 4 additions & 1 deletion v2/internal/pkg/cli/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestExecutorMirroring(t *testing.T) {
fakeStorageInterruptChan := make(chan error)
go skipSignalsToInterruptStorage(fakeStorageInterruptChan)

cr := MockClusterResources{}
opts := &mirror.CopyOptions{
Global: global,
DeprecatedTLSVerify: deprecatedTLSVerifyOpt,
Expand Down Expand Up @@ -108,6 +109,7 @@ func TestExecutorMirroring(t *testing.T) {
localStorageInterruptChannel: fakeStorageInterruptChan,
MakeDir: MakeDir{},
LogsDir: "/tmp/",
ClusterResources: cr,
}

res := &cobra.Command{}
Expand Down Expand Up @@ -142,6 +144,7 @@ func TestExecutorMirroring(t *testing.T) {
localStorageInterruptChannel: fakeStorageInterruptChan,
MakeDir: MakeDir{},
LogsDir: "/tmp/",
ClusterResources: cr,
}

res := &cobra.Command{}
Expand Down Expand Up @@ -1115,7 +1118,7 @@ func (o MockClusterResources) UpdateServiceGenerator(graphImage, releaseImage st
func (o MockClusterResources) CatalogSourceGenerator(allRelatedImages []v2alpha1.CopyImageSchema) error {
return nil
}
func (o MockClusterResources) GenerateSignatureConfigMap(digest string, id int, data []byte) error {
func (o MockClusterResources) GenerateSignatureConfigMap() error {
return nil
}

Expand Down
56 changes: 36 additions & 20 deletions v2/internal/pkg/clusterresources/clusterresources.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ func toRFC1035(r rune) rune {
}
}

func (o *ClusterResourcesGenerator) GenerateSignatureConfigMap(digest string, id int, data []byte) error {
func (o *ClusterResourcesGenerator) GenerateSignatureConfigMap() error {
o.Log.Info("📄 Generating Signature Configmap...")
// create and store config map
cm := &cm.ConfigMap{
Expand All @@ -624,28 +624,44 @@ func (o *ClusterResourcesGenerator) GenerateSignatureConfigMap(digest string, id
},
BinaryData: make(map[string]string),
}
// base64 encode data
b64 := base64.RawStdEncoding.EncodeToString(data)
index := fmt.Sprintf(configMapBinaryDataIndexFormat, digest, id+1)
cm.BinaryData[index] = b64
jsonData, err := json.Marshal(cm)
// read the signatures directory
sigDir := filepath.Join(o.WorkingDir, signatureDir)
signatures, err := os.ReadDir(sigDir)
if err != nil {
return fmt.Errorf("[GenerateSignatureConfigMap] %v", err)
return fmt.Errorf(signatureConfigMapMsg, err)
}
// write to cluster-resources directory
ferr := os.WriteFile(filepath.Join(o.WorkingDir, clusterResourcesDir)+"/"+index+".json", jsonData, 0644)
if ferr != nil {
return fmt.Errorf("[GenerateSignatureConfigMap] %v", ferr)
}
yamlData, err := yaml.Marshal(cm)
if err != nil {
return fmt.Errorf("[GenerateSignatureConfigMap] %v", err)
}
// write to cluster-resources directory
ferr = os.WriteFile(filepath.Join(o.WorkingDir, clusterResourcesDir)+"/"+index+".yaml", yamlData, 0644)
if ferr != nil {
return fmt.Errorf("[GenerateSignatureConfigMap] %v", ferr)

if len(signatures) == 0 {
return fmt.Errorf(signatureConfigMapMsg, "signature files not found, could not generate signature configmap")
}

for id, file := range signatures {
data, err := os.ReadFile(sigDir + "/" + file.Name())
if err != nil {
return fmt.Errorf(signatureConfigMapMsg, err)
}
// base64 encode data
b64 := base64.RawStdEncoding.EncodeToString(data)
index := fmt.Sprintf(configMapBinaryDataIndexFormat, file.Name(), id+1)
cm.BinaryData[index] = b64
jsonData, err := json.Marshal(cm)
if err != nil {
return fmt.Errorf(signatureConfigMapMsg, err)
}
// write to cluster-resources directory
ferr := os.WriteFile(filepath.Join(o.WorkingDir, clusterResourcesDir)+"/"+index+".json", jsonData, 0644)
if ferr != nil {
return fmt.Errorf(signatureConfigMapMsg, ferr)
}
yamlData, err := yaml.Marshal(cm)
if err != nil {
return fmt.Errorf(signatureConfigMapMsg, err)
}
// write to cluster-resources directory
ferr = os.WriteFile(filepath.Join(o.WorkingDir, clusterResourcesDir)+"/"+index+".yaml", yamlData, 0644)
if ferr != nil {
return fmt.Errorf(signatureConfigMapMsg, ferr)
}
}
return nil
}
18 changes: 11 additions & 7 deletions v2/internal/pkg/clusterresources/clusterresources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
updateservicev1 "github.com/openshift/oc-mirror/v2/internal/pkg/clusterresources/updateservice/v1"
"github.com/openshift/oc-mirror/v2/internal/pkg/common"
clog "github.com/openshift/oc-mirror/v2/internal/pkg/log"
"github.com/otiai10/copy"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -1019,23 +1020,26 @@ func TestGenerateSignatureConfigMap(t *testing.T) {
tmpDir := t.TempDir()
workingDir := filepath.Join(tmpDir, "working-dir")
err := os.MkdirAll(workingDir+"/"+clusterResourcesDir, 0755)
err = os.MkdirAll(workingDir+"/"+signatureDir, 0755)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(workingDir)

err = copy.Copy("../../../tests/37433b71c073c6cbfc8173ec7ab2d99032c8e6d6fe29de06e062d85e33e34531",
workingDir+"/"+signatureDir+"/37433b71c073c6cbfc8173ec7ab2d99032c8e6d6fe29de06e062d85e33e34531")
if err != nil {
t.Fatal(err)
}
//defer os.RemoveAll(workingDir)

log := clog.New("trace")
cmJson := cm.ConfigMap{}
cr := &ClusterResourcesGenerator{
Log: log,
WorkingDir: workingDir,
}
data, err := os.ReadFile("../../../tests/test.sig")
if err != nil {
t.Fatal(err)
}
digest := "37433b71c073c6cbfc8173ec7ab2d99032c8e6d6fe29de06e062d85e33e34531"
err = cr.GenerateSignatureConfigMap(digest, 0, data)

err = cr.GenerateSignatureConfigMap()
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 2 additions & 0 deletions v2/internal/pkg/clusterresources/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ const (
configMapBinaryDataIndexFormat = "sha256-%s-%d"
signatureNamespace = "openshift-config-managed"
signatureLabel = "release.openshift.io/verification-signatures"
signatureConfigMapMsg = "[GenerateSignatureConfigMap] %v"
signatureDir = "signatures"
)
2 changes: 1 addition & 1 deletion v2/internal/pkg/clusterresources/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ type GeneratorInterface interface {
IDMS_ITMSGenerator(allRelatedImages []v2alpha1.CopyImageSchema, forceRepositoryScope bool) error
UpdateServiceGenerator(graphImage, releaseImage string) error
CatalogSourceGenerator(allRelatedImages []v2alpha1.CopyImageSchema) error
GenerateSignatureConfigMap(digest string, id int, data []byte) error
GenerateSignatureConfigMap() error
}
11 changes: 2 additions & 9 deletions v2/internal/pkg/release/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"time"

"github.com/openshift/oc-mirror/v2/internal/pkg/api/v2alpha1"
"github.com/openshift/oc-mirror/v2/internal/pkg/clusterresources"
//"github.com/openshift/oc-mirror/v2/internal/pkg/clusterresources"
"github.com/openshift/oc-mirror/v2/internal/pkg/image"
clog "github.com/openshift/oc-mirror/v2/internal/pkg/log"
"github.com/openshift/oc-mirror/v2/internal/pkg/mirror"
Expand Down Expand Up @@ -60,7 +60,7 @@ func (o SignatureSchema) GenerateReleaseSignatures(ctx context.Context, images [
}
httpClient := &http.Client{Transport: tr}

for id, img := range images {
for _, img := range images {
imgSpec, err := image.ParseRef(img.Source)
if err != nil {
return []v2alpha1.CopyImageSchema{}, fmt.Errorf("parsing image digest")
Expand Down Expand Up @@ -175,13 +175,6 @@ func (o SignatureSchema) GenerateReleaseSignatures(ctx context.Context, images [
o.Log.Error("%v", ferr)
}
imgs = append(imgs, img)

cr := clusterresources.New(o.Log, o.Opts.Global.WorkingDir, o.Config, "")
err = cr.GenerateSignatureConfigMap(digest, id, data)
if err != nil {
o.Log.Error("%v", err)
}

} else {
o.Log.Warn("no signature found for %s", digest)
return []v2alpha1.CopyImageSchema{}, fmt.Errorf("no signature found for %s image %s", digest, img.Source)
Expand Down
File renamed without changes.

0 comments on commit 783ff4b

Please sign in to comment.