| 13 | ) |
| 14 | |
| 15 | func PreProcess(p provider.Provider, inv inventory.InventoryInfo, strategy common.DryRunStrategy) (inventory.InventoryPolicy, error) { |
| 16 | invClient, err := p.InventoryClient() |
| 17 | if err != nil { |
| 18 | return inventory.InventoryPolicyMustMatch, err |
| 19 | } |
| 20 | obj, err := invClient.GetClusterInventoryInfo(inv) |
| 21 | if err != nil { |
| 22 | if apierrors.IsNotFound(err) { |
| 23 | return inventory.InventoryPolicyMustMatch, nil |
| 24 | } |
| 25 | return inventory.InventoryPolicyMustMatch, err |
| 26 | } |
| 27 | |
| 28 | if obj == nil { |
| 29 | return inventory.InventoryPolicyMustMatch, nil |
| 30 | } |
| 31 | |
| 32 | managedByKey := "apps.kubernetes.io/managed-by" |
| 33 | managedByVal := "kpt" |
| 34 | labels := obj.GetLabels() |
| 35 | val, found := labels[managedByKey] |
| 36 | if found { |
| 37 | if val != managedByVal { |
| 38 | return inventory.InventoryPolicyMustMatch, fmt.Errorf("can't apply the current package since it is managed by %s", val) |
| 39 | } |
| 40 | return inventory.InventoryPolicyMustMatch, nil |
| 41 | } |
| 42 | labels[managedByKey] = managedByVal |
| 43 | if strategy.ClientOrServerDryRun() { |
| 44 | return inventory.AdoptIfNoInventory, nil |
| 45 | } |
| 46 | err = invClient.UpdateLabels(inv, labels) |
| 47 | return inventory.AdoptIfNoInventory, err |
| 48 | } |