// Reconcile reconciles the SSH keys of a Tenant resource.
Reconcile reconciles the SSH keys of a Tenant resource.
// Reconcile reconciles the SSH keys of a Tenant resource .
func (r *BastionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
	ctx := context.Background()
	log := r.Log.WithValues("tenant", req.NamespacedName)
	log.Info("reconciling bastion")
	tenant := &crownlabsalpha1.Tenant{}
	deleted := false
	if err := r.Get(ctx, req.NamespacedName, tenant); apierrors.IsNotFound(err) {
		deleted = true
	} else if err != nil {
		return ctrl.Result{}, err
	}
	var keys []string
	if _, err := os.Stat(r.AuthorizedKeysPath); err == nil {
		data, err := ioutil.ReadFile(r.AuthorizedKeysPath)
		if err != nil {
			log.Error(err, "unable to read the file authorized_keys")
			return ctrl.Result{}, err
		}
		if len(data) > 0 {
			keys = decomposeAndPurgeEntries(strings.Split(string(data), string("\n")), req.NamespacedName.Name)
		}
	}
	if !deleted {
		keys = composeAndMarkEntries(keys, tenant.Spec.PublicKeys, req.NamespacedName.Name)
	}
	f, err := os.Create(r.AuthorizedKeysPath)
	if err != nil {
		log.Error(err, "unable to create the file authorized_keys")
		return ctrl.Result{}, nil
	}
	defer closeFile(f, log)
	if len(keys) > 0 {
		_, err = f.Write([]byte(strings.Join(keys, string("\n"))))
		if err != nil {
			log.Error(err, "unable to write to authorized_keys")
			return ctrl.Result{}, nil
		}
	}
	return ctrl.Result{}, nil
}
func ( r * BastionReconciler ) Reconcile ( req ctrl . Request ) ( ctrl . Result , error ) { ctx := context . Background ( ) log := r . Log . WithValues ( " " , req . NamespacedName ) log . Info ( " " ) tenant := & crownlabsalpha1 . Tenant { } deleted := false if err := r . Get ( ctx , req . NamespacedName , tenant ) ; apierrors . IsNotFound ( err ) { deleted = true } else if err != nil { return ctrl . Result { } , err } var keys [ ] string if _ , err := os . Stat ( r . AuthorizedKeysPath ) ; err == nil { data , err := ioutil . ReadFile ( r . AuthorizedKeysPath ) if err != nil { log . Error ( err , " " ) return ctrl . Result { } , err } if len ( data ) > 0 { keys = decomposeAndPurgeEntries ( strings . Split ( string ( data ) , string ( " \n " ) ) , req . NamespacedName . Name ) } } if ! deleted { keys = composeAndMarkEntries ( keys , tenant . Spec . PublicKeys , req . NamespacedName . Name ) } f , err := os . Create ( r . AuthorizedKeysPath ) if err != nil { log . Error ( err , " " ) return ctrl . Result { } , nil } defer closeFile ( f , log ) if len ( keys ) > 0 { _ , err = f . Write ( [ ] byte ( strings . Join ( keys , string ( " \n " ) ) ) ) if err != nil { log . Error ( err , " " ) return ctrl . Result { } , nil } } return ctrl . Result { } , nil }
Reconcile reconciles the SSH keys of a Tenant resource.
Reconcile reconciles the SSH keys of a Tenant resource .
// if the file exists, read the whole file in a []byte // if the event was NOT a deletion, add the tenant's keys. Otherwise nothing to do.