github.com/opendevstack/tailor@v1.3.5-0.20220119161809-cab064e60a67/pkg/openshift/change.go (about)

     1  package openshift
     2  
     3  import (
     4  	"github.com/pmezard/go-difflib/difflib"
     5  )
     6  
     7  var (
     8  	kindToShortMapping = map[string]string{
     9  		"Service":                 "svc",
    10  		"Route":                   "route",
    11  		"DeploymentConfig":        "dc",
    12  		"Deployment":              "deployment",
    13  		"BuildConfig":             "bc",
    14  		"ImageStream":             "is",
    15  		"PersistentVolumeClaim":   "pvc",
    16  		"Template":                "template",
    17  		"ConfigMap":               "cm",
    18  		"Secret":                  "secret",
    19  		"RoleBinding":             "rolebinding",
    20  		"ServiceAccount":          "serviceaccount",
    21  		"CronJob":                 "cronjob",
    22  		"Job":                     "job",
    23  		"LimitRange":              "limitrange",
    24  		"ResourceQuota":           "quota",
    25  		"HorizontalPodAutoscaler": "hpa",
    26  		"StatefulSet":             "statefulset",
    27  	}
    28  )
    29  
    30  // Change is a description of a drift between current and desired state, and
    31  // the required patches to bring them back in sync.
    32  type Change struct {
    33  	Action       string
    34  	Kind         string
    35  	Name         string
    36  	CurrentState string
    37  	DesiredState string
    38  }
    39  
    40  // NewChange creates a new change for given template/platform item.
    41  func NewChange(templateItem *ResourceItem, platformItem *ResourceItem) *Change {
    42  	c := &Change{
    43  		Kind:         templateItem.Kind,
    44  		Name:         templateItem.Name,
    45  		CurrentState: platformItem.YamlConfig(),
    46  		DesiredState: templateItem.YamlConfig(),
    47  	}
    48  
    49  	if platformItem.YamlConfig() != templateItem.YamlConfig() {
    50  		c.Action = "Update"
    51  	} else {
    52  		c.Action = "Noop"
    53  	}
    54  
    55  	return c
    56  }
    57  
    58  // ItemName returns the kind/name of the resource the change relates to.
    59  func (c *Change) ItemName() string {
    60  	return kindToShortMapping[c.Kind] + "/" + c.Name
    61  }
    62  
    63  // Diff returns a unified diff text for the change.
    64  func (c *Change) Diff(revealSecrets bool) string {
    65  	if c.isSecret() && !revealSecrets {
    66  		return "Secret drift is hidden. Use --reveal-secrets to see details.\n"
    67  	}
    68  	diff := difflib.UnifiedDiff{
    69  		A:        difflib.SplitLines(c.CurrentState),
    70  		B:        difflib.SplitLines(c.DesiredState),
    71  		FromFile: "Current State (OpenShift cluster)",
    72  		ToFile:   "Desired State (Processed template)",
    73  		Context:  3,
    74  	}
    75  	text, _ := difflib.GetUnifiedDiffString(diff)
    76  	return text
    77  }
    78  
    79  func (c *Change) isSecret() bool {
    80  	return kindToShortMapping[c.Kind] == "secret"
    81  }
    82  
    83  func recreateChanges(templateItem, platformItem *ResourceItem) []*Change {
    84  	deleteChange := &Change{
    85  		Action:       "Delete",
    86  		Kind:         templateItem.Kind,
    87  		Name:         templateItem.Name,
    88  		CurrentState: platformItem.YamlConfig(),
    89  		DesiredState: "",
    90  	}
    91  	createChange := &Change{
    92  		Action:       "Create",
    93  		Kind:         templateItem.Kind,
    94  		Name:         templateItem.Name,
    95  		CurrentState: "",
    96  		DesiredState: templateItem.YamlConfig(),
    97  	}
    98  	return []*Change{deleteChange, createChange}
    99  }