Skip to content

v1.1.1 patch #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/graphman/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,13 @@ func AddMappings(src []byte, dest []byte) ([]byte, error) {
}

func matchOptionsLevelFormat(value string) string {
re := regexp.MustCompile(`{{(.*)}}`)
re := regexp.MustCompile(`^{{(.*)}}`)
match := re.FindStringSubmatch(value)
if len(match) > 1 {
return match[1]

} else {
re := regexp.MustCompile(`{(.*)}`)
re := regexp.MustCompile(`^{(.*)}`)
match = re.FindStringSubmatch(value)
if len(match) > 1 {
return match[1]
Expand Down
2 changes: 1 addition & 1 deletion internal/graphman/generated-modified.go
Original file line number Diff line number Diff line change
Expand Up @@ -2339,7 +2339,7 @@ type PolicyBackedIdpInput struct {
// Authentication Policy Name
AuthPolicyName string `json:"authPolicyName"`
// Default Role
DefaultRoleName string `json:"defaultRoleName"`
DefaultRoleName string `json:"defaultRoleName,omitempty"`
// Additional properties
Properties []*EntityPropertyInput `json:"properties,omitempty"`
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/repository/reconcile/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ func syncRepository(ctx context.Context, params Params) error {
if err != nil {
params.Log.V(2).Info("failed to reconcile storage secret", "name", repository.Name+"-repository", "namespace", repository.Namespace, "error", err.Error())
storageSecretName = ""
if err.Error() == "exceededMaxSize" {
storageSecretName = "_"
}

}

repoStatus.Commit = commit
Expand All @@ -183,7 +187,9 @@ func syncRepository(ctx context.Context, params Params) error {
repoStatus.Summary = repository.Spec.Endpoint
}

repoStatus.StorageSecretName = storageSecretName
if repoStatus.StorageSecretName != "_" {
repoStatus.StorageSecretName = storageSecretName
}

if !reflect.DeepEqual(repoStatus, repository.Status) {
params.Log.Info("syncing repository", "name", repository.Name, "namespace", repository.Namespace)
Expand Down
58 changes: 57 additions & 1 deletion pkg/repository/reconcile/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package reconcile

import (
"context"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"sync"

v1 "github.com/caapim/layer7-operator/api/v1"
"github.com/caapim/layer7-operator/pkg/repository"
Expand Down Expand Up @@ -54,7 +58,12 @@ func StorageSecret(ctx context.Context, params Params) error {
if ext == "" {
ext = params.Instance.Spec.Tag
}
switch strings.ToLower(params.Instance.Spec.Type) {

if params.Instance.Status.StorageSecretName == "_" {
return nil
}

switch strings.ToLower(string(params.Instance.Spec.Type)) {
case "http":
fileURL, err := url.Parse(params.Instance.Spec.Endpoint)
if err != nil {
Expand All @@ -75,7 +84,14 @@ func StorageSecret(ctx context.Context, params Params) error {
default:
params.Log.Info("repository type not set", "name", params.Instance.Name, "namespace", params.Instance.Name)
return nil
}

dirSize, err := getDirSize("/tmp/" + params.Instance.Name + "-" + params.Instance.Namespace + "-" + ext)
if err != nil {
return err
}
if dirSize/1000.0/1000.0/3.5 > 0.9 {
return errors.New("exceededMaxSize")
}

bundleGzip, err := util.CompressGraphmanBundle("/tmp/" + params.Instance.Name + "-" + params.Instance.Namespace + "-" + ext)
Expand All @@ -96,6 +112,46 @@ func StorageSecret(ctx context.Context, params Params) error {
return nil
}

func getDirSize(path string) (sizef float64, err error) {
var size int64
var mu sync.Mutex

var calculateSize func(string) error
calculateSize = func(p string) error {
fileInfo, err := os.Lstat(p)
if err != nil {
return err
}

if fileInfo.Mode()&os.ModeSymlink != 0 {
return nil
}

if fileInfo.IsDir() {
entries, err := os.ReadDir(p)
if err != nil {
return err
}
for _, entry := range entries {
if err := calculateSize(filepath.Join(p, entry.Name())); err != nil {
return err
}
}
} else {
mu.Lock()
size += fileInfo.Size()
mu.Unlock()
}
return nil
}

if err := calculateSize(path); err != nil {
return 0, err
}

return float64(size), nil
}

func reconcileSecret(ctx context.Context, params Params, desiredSecret *corev1.Secret) error {

if err := controllerutil.SetControllerReference(params.Instance, desiredSecret, params.Scheme); err != nil {
Expand Down
35 changes: 35 additions & 0 deletions pkg/util/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package util
import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"fmt"
"io"
Expand Down Expand Up @@ -150,3 +151,37 @@ func Untar(folderName string, repoName string, tarStream io.Reader, gz bool) err
}
return nil
}

func GzipDecompress(gzipBundle []byte) (bundleBytes []byte, err error) {
r := bytes.NewReader(gzipBundle)
gzr, err := gzip.NewReader(r)
if err != nil {
return nil, err
}

bundleBytes, err = io.ReadAll(gzr)
if err != nil {
return nil, err
}

return bundleBytes, nil
}

func GzipCompress(gzipBundle []byte) (gzipBytes []byte, err error) {

var buf bytes.Buffer
zw := gzip.NewWriter(&buf)
_, err = zw.Write(gzipBundle)
if err != nil {
return nil, err
}

if err := zw.Close(); err != nil {
return nil, err
}

gzipBytes = buf.Bytes()
buf.Reset()

return gzipBytes, nil
}
49 changes: 33 additions & 16 deletions pkg/util/graphman.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func CompressGraphmanBundle(path string) ([]byte, error) {
}
if buf.Len() > 900000 {
buf.Reset()
return nil, errors.New("this bundle would exceed the maximum Kubernetes secret size")
return nil, errors.New("exceededMaxSize")
}

compressedBundle := buf.Bytes()
Expand All @@ -356,40 +356,46 @@ func CompressGraphmanBundle(path string) ([]byte, error) {
return compressedBundle, nil
}

func ConcatBundles(bundleMap map[string][]byte) ([]byte, error) {
var combinedBundle []byte

func ConcatBundles(bundleMap map[string][]byte) (combinedBundle []byte, err error) {
for k, bundle := range bundleMap {
if strings.HasSuffix(k, ".json") {
newBundle, err := graphman.ConcatBundle(combinedBundle, bundle)
combinedBundle, err = graphman.ConcatBundle(combinedBundle, bundle)
if err != nil {
return nil, err
}
}
if strings.HasSuffix(k, ".gz") {
bundle, err := GzipDecompress(bundle)
if err != nil {
return nil, err
}
combinedBundle, err = graphman.ConcatBundle(combinedBundle, bundle)
if err != nil {
return nil, err
}
combinedBundle = newBundle
}
if k == "bundle-properties.json" {
newBundle, err := graphman.AddMappings(combinedBundle, bundle)
combinedBundle, err = graphman.AddMappings(combinedBundle, bundle)
if err != nil {
return nil, err
}
combinedBundle = newBundle
}
}

return combinedBundle, nil

}

func BuildAndValidateBundle(path string) ([]byte, error) {
func BuildAndValidateBundle(path string) (bundleBytes []byte, err error) {
if path == "" {
return nil, nil
}
bundle := graphman.Bundle{}

if _, err := os.Stat(path); err != nil {
return nil, err
}

bundleBytes, err := graphman.Implode(path)
bundleBytes, err = graphman.Implode(path)
if err != nil {
return nil, err
}
Expand All @@ -402,16 +408,27 @@ func BuildAndValidateBundle(path string) ([]byte, error) {
segments := strings.Split(d.Name(), ".")
ext := segments[len(segments)-1]
if ext == "json" && !strings.Contains(strings.ToLower(d.Name()), "sourcesummary.json") && !strings.Contains(strings.ToLower(d.Name()), "bundle-properties.json") {
//sbb := bundleBytes
srcBundleBytes, err := os.ReadFile(path)
if err != nil {
return err
}
sbb, err := graphman.ConcatBundle(srcBundleBytes, bundleBytes)
tb := graphman.Bundle{}
err = json.Unmarshal(srcBundleBytes, &tb)
if err != nil {
return nil
}
bundleBytes = sbb
tbb, err := json.Marshal(tb)
if err != nil {
return nil
}

if len(tbb) > 2 {
sbb, err := graphman.ConcatBundle(srcBundleBytes, bundleBytes)
if err != nil {
return nil
}
bundleBytes = sbb
}
}
}
return nil
Expand All @@ -422,12 +439,12 @@ func BuildAndValidateBundle(path string) ([]byte, error) {
if len(bundleBytes) <= 2 {
return nil, errors.New("no valid graphman bundles were found")
}

bundle := graphman.Bundle{}
r := bytes.NewReader(bundleBytes)
d := json.NewDecoder(r)
d.DisallowUnknownFields()
_ = json.Unmarshal(bundleBytes, &bundle)
// check the graphman bundle for errors

err = d.Decode(&bundle)
if err != nil {
return nil, err
Expand Down
Loading