Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Fahed DORGAA <fahed.dorgaa@gmail.com>
  • Loading branch information
fahedouch committed May 16, 2022
1 parent a743b64 commit f15a085
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions internal/controller/ansibleRun/ansibleRun.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,20 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
)

const (
// AnnotationKeyPolicyRun is the name of an annotation which instructs
// the provider how to run the corresponding Ansible contents
AnnotationKeyPolicyRun = "ansible.crossplane.io/runPolicy"
)

const (
errNotAnsibleRun = "managed resource is not a AnsibleRun custom resource"
errTrackPCUsage = "cannot track ProviderConfig usage"
Expand Down Expand Up @@ -212,13 +219,6 @@ func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.E
}
}

// Requirements is a list of collections to be installed, it is stored in requirements file
if pc.Spec.Requirements != nil {
if err := c.fs.WriteFile(filepath.Join(dir, galaxyutil.RequirementsFile), []byte(*pc.Spec.Requirements), 0600); err != nil {
return nil, errors.Wrap(err, errWriteConfig)
}
}

// Committing the AnsibleRun's desired state (contentVars) to the filesystem at p.WorkingDirPath.
contentVars := map[string]interface{}{}
if len(cr.Spec.ForProvider.Vars) != 0 {
Expand All @@ -232,13 +232,27 @@ func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.E
}

ps := c.ansible(dir)

// prepare ansible extravars
ansibleEnvDir := filepath.Clean(filepath.Join(dir, "env"))
if err := c.fs.MkdirAll(ansibleEnvDir, 0700); resource.Ignore(os.IsExist, err) != nil {
return nil, errors.Wrap(err, errMkdir)
}
if err := ps.AddFile("env/extravars", contentVarsBytes); err != nil {
return nil, err
}
// install ansible requirements using ansible-galaxy
if err := ps.GalaxyInstall(ctx); err != nil {
return nil, err

// Requirements is a list of collections to be installed, it is stored in requirements file
if pc.Spec.Requirements != nil {
if err := c.fs.WriteFile(filepath.Join(dir, galaxyutil.RequirementsFile), []byte(*pc.Spec.Requirements), 0600); err != nil {
return nil, errors.Wrap(err, errWriteConfig)
}
// install ansible requirements using ansible-galaxy
if err := ps.GalaxyInstall(ctx); err != nil {
return nil, err
}
}

r, err := ps.Init(ctx, cr, pc)
if err != nil {
return nil, errors.Wrap(err, errInit)
Expand Down Expand Up @@ -349,6 +363,8 @@ func getLastApplied(observed *v1alpha1.AnsibleRun) (*v1alpha1.AnsibleRun, error)
return last, nil
}

// nolint: gocyclo
// TODO reduce cyclomatic complexity
func (c *external) handleLastApplied(last, desired *v1alpha1.AnsibleRun) (managed.ExternalObservation, error) {
isUpToDate := false

Expand Down Expand Up @@ -400,3 +416,13 @@ func (c *external) handleLastApplied(last, desired *v1alpha1.AnsibleRun) (manage
}
return desired, nil
}*/

// GetPolicyRun returns the ansible run policy annotation value on the resource.
func GetPolicyRun(o metav1.Object) string {
return o.GetAnnotations()[AnnotationKeyPolicyRun]
}

// SetPolicyRun sets the ansible run policy annotation of the resource.
func SetPolicyRun(o metav1.Object, name string) {
meta.AddAnnotations(o, map[string]string{AnnotationKeyPolicyRun: name})
}

0 comments on commit f15a085

Please sign in to comment.