Skip to content

Commit

Permalink
working to validate file
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Apr 30, 2024
1 parent dd43a14 commit e8b084d
Showing 1 changed file with 135 additions and 2 deletions.
137 changes: 135 additions & 2 deletions internal/pkg/rpaas/validation/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ package validation
import (
"context"
"errors"
"fmt"
"time"

"github.com/tsuru/rpaas-operator/api/v1alpha1"
"github.com/tsuru/rpaas-operator/internal/pkg/rpaas"
corev1 "k8s.io/api/core/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -70,15 +73,145 @@ func (v *validationManager) UpdateBlock(ctx context.Context, instanceName string
}

func (v *validationManager) CreateExtraFiles(ctx context.Context, instanceName string, files ...rpaas.File) error {
return errNotImplementedYet
validation, err := v.validationCRD(ctx, instanceName)
if err != nil {
return err
}

tempConfigMaps := []*corev1.ConfigMap{}
defer func() {
for _, configMap := range tempConfigMaps {
deleteErr := v.cli.Delete(ctx, configMap)
if deleteErr != nil {

Check failure on line 85 in internal/pkg/rpaas/validation/manager.go

View workflow job for this annotation

GitHub Actions / test

SA9003: empty branch (staticcheck)
// TODO: print err
}
}

}()

for _, f := range files {
_, found := findFileByName(validation.Spec.Files, f.Name)
if found {
return rpaas.ErrExtraFileAlreadyExists
}
}

if validation.Spec.Files == nil {
validation.Spec.Files = make([]v1alpha1.File, 0, len(files))
}

for _, f := range files {
configMap, configMapErr := v.createTemporaryFileInConfigMap(ctx, validation, f)
if configMapErr != nil {
return configMapErr
}

tempConfigMaps = append(tempConfigMaps, configMap)

validation.Spec.Files = append(validation.Spec.Files, v1alpha1.File{
Name: f.Name,
ConfigMap: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: configMap.Name},
Key: f.Name,
},
})
}

err = v.waitController(ctx, validation)
if err != nil {
return err
}

return v.RpaasManager.CreateExtraFiles(ctx, instanceName, files...)
}

func (v *validationManager) DeleteExtraFiles(ctx context.Context, instanceName string, filenames ...string) error {
return errNotImplementedYet
}

func (v *validationManager) UpdateExtraFiles(ctx context.Context, instanceName string, files ...rpaas.File) error {
return errNotImplementedYet
validation, err := v.validationCRD(ctx, instanceName)
if err != nil {
return err
}

tempConfigMaps := []*corev1.ConfigMap{}
defer func() {
for _, configMap := range tempConfigMaps {
deleteErr := v.cli.Delete(ctx, configMap)
if deleteErr != nil {

Check failure on line 142 in internal/pkg/rpaas/validation/manager.go

View workflow job for this annotation

GitHub Actions / test

SA9003: empty branch (staticcheck)
// TODO: print err
}
}

}()

for _, f := range files {
position, found := findFileByName(validation.Spec.Files, f.Name)
if !found {
return rpaas.ErrNoSuchExtraFile
}

configMap, configMapErr := v.createTemporaryFileInConfigMap(ctx, validation, f)
if configMapErr != nil {
return configMapErr
}

tempConfigMaps = append(tempConfigMaps, configMap)

validation.Spec.Files[position] = v1alpha1.File{
Name: f.Name,
ConfigMap: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: configMap.Name},
Key: f.Name,
},
}
}

err = v.waitController(ctx, validation)
if err != nil {
return err
}

return v.RpaasManager.UpdateExtraFiles(ctx, instanceName, files...)
}

func (m *validationManager) createTemporaryFileInConfigMap(ctx context.Context, validation *v1alpha1.RpaasValidation, f rpaas.File) (*corev1.ConfigMap, error) {
newConfigMap := newConfigMapForFile(validation, f)
if err := m.cli.Create(ctx, newConfigMap); err != nil {
return nil, err
}

return newConfigMap, nil

}

func newConfigMapForFile(validation *v1alpha1.RpaasValidation, f rpaas.File) *corev1.ConfigMap {
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
GenerateName: fmt.Sprintf("%s-validation-file-", validation.Name),
Namespace: validation.Namespace,
// we need TO discover UID of validation early
/*OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(validation, schema.GroupVersionKind{
Group: v1alpha1.GroupVersion.Group,
Version: v1alpha1.GroupVersion.Version,
Kind: "RpaasValidation",
}),
},*/
},
BinaryData: map[string][]byte{f.Name: f.Content},
}
}

func findFileByName(files []v1alpha1.File, filename string) (int, bool) {
for i := range files {
if files[i].Name == filename {
return i, true
}
}

return -1, false
}

func (v *validationManager) DeleteRoute(ctx context.Context, instanceName, path string) error {
Expand Down

0 comments on commit e8b084d

Please sign in to comment.