github.com/tigera/api@v0.0.0-20240320170621-278e89a8c5fb/pkg/apis/projectcalico/v3/conversion.go (about)

     1  // Copyright (c) 2019-2022 Tigera, Inc. All rights reserved.
     2  
     3  package v3
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"k8s.io/apimachinery/pkg/runtime"
     9  	"k8s.io/apimachinery/pkg/runtime/schema"
    10  )
    11  
    12  func addConversionFuncs(scheme *runtime.Scheme) error {
    13  	// Add non-generated conversion functions
    14  	err := scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "NetworkPolicy"},
    15  		func(label, value string) (string, string, error) {
    16  			switch label {
    17  			case "spec.tier", "metadata.name", "metadata.namespace":
    18  				return label, value, nil
    19  			default:
    20  				return "", "", fmt.Errorf("field label not supported: %s", label)
    21  			}
    22  		},
    23  	)
    24  	if err != nil {
    25  		return err
    26  	}
    27  
    28  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "GlobalNetworkPolicy"},
    29  		func(label, value string) (string, string, error) {
    30  			switch label {
    31  			case "spec.tier", "metadata.name":
    32  				return label, value, nil
    33  			default:
    34  				return "", "", fmt.Errorf("field label not supported: %s", label)
    35  			}
    36  		},
    37  	)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "GlobalNetworkSet"},
    43  		func(label, value string) (string, string, error) {
    44  			switch label {
    45  			case "metadata.name":
    46  				return label, value, nil
    47  			default:
    48  				return "", "", fmt.Errorf("field label not supported: %s", label)
    49  			}
    50  		},
    51  	)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "NetworkSet"},
    57  		func(label, value string) (string, string, error) {
    58  			switch label {
    59  			case "metadata.name", "metadata.namespace":
    60  				return label, value, nil
    61  			default:
    62  				return "", "", fmt.Errorf("field label not supported: %s", label)
    63  			}
    64  		},
    65  	)
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "HostEndpoint"},
    71  		func(label, value string) (string, string, error) {
    72  			switch label {
    73  			case "metadata.name":
    74  				return label, value, nil
    75  			default:
    76  				return "", "", fmt.Errorf("field label not supported: %s", label)
    77  			}
    78  		},
    79  	)
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "IPPool"},
    85  		func(label, value string) (string, string, error) {
    86  			switch label {
    87  			case "metadata.name":
    88  				return label, value, nil
    89  			default:
    90  				return "", "", fmt.Errorf("field label not supported: %s", label)
    91  			}
    92  		},
    93  	)
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "BGPConfiguration"},
    99  		func(label, value string) (string, string, error) {
   100  			switch label {
   101  			case "metadata.name":
   102  				return label, value, nil
   103  			default:
   104  				return "", "", fmt.Errorf("field label not supported: %s", label)
   105  			}
   106  		},
   107  	)
   108  	if err != nil {
   109  		return err
   110  	}
   111  
   112  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "BGPPeer"},
   113  		func(label, value string) (string, string, error) {
   114  			switch label {
   115  			case "metadata.name":
   116  				return label, value, nil
   117  			default:
   118  				return "", "", fmt.Errorf("field label not supported: %s", label)
   119  			}
   120  		},
   121  	)
   122  	if err != nil {
   123  		return err
   124  	}
   125  
   126  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "Profile"},
   127  		func(label, value string) (string, string, error) {
   128  			switch label {
   129  			case "metadata.name":
   130  				return label, value, nil
   131  			default:
   132  				return "", "", fmt.Errorf("field label not supported: %s", label)
   133  			}
   134  		},
   135  	)
   136  	if err != nil {
   137  		return err
   138  	}
   139  
   140  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "FelixConfiguration"},
   141  		func(label, value string) (string, string, error) {
   142  			switch label {
   143  			case "metadata.name":
   144  				return label, value, nil
   145  			default:
   146  				return "", "", fmt.Errorf("field label not supported: %s", label)
   147  			}
   148  		},
   149  	)
   150  	if err != nil {
   151  		return err
   152  	}
   153  
   154  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "KubeControllersConfiguration"},
   155  		func(label, value string) (string, string, error) {
   156  			switch label {
   157  			case "metadata.name":
   158  				return label, value, nil
   159  			default:
   160  				return "", "", fmt.Errorf("field label not supported: %s", label)
   161  			}
   162  		},
   163  	)
   164  	if err != nil {
   165  		return err
   166  	}
   167  
   168  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "ClusterInformation"},
   169  		func(label, value string) (string, string, error) {
   170  			switch label {
   171  			case "metadata.name":
   172  				return label, value, nil
   173  			default:
   174  				return "", "", fmt.Errorf("field label not supported: %s", label)
   175  			}
   176  		},
   177  	)
   178  	if err != nil {
   179  		return err
   180  	}
   181  
   182  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "GlobalAlert"},
   183  		func(label, value string) (string, string, error) {
   184  			switch label {
   185  			case "metadata.name":
   186  				return label, value, nil
   187  			default:
   188  				return "", "", fmt.Errorf("field label not supported: %s", label)
   189  			}
   190  		},
   191  	)
   192  	if err != nil {
   193  		return err
   194  	}
   195  
   196  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "GlobalAlertTemplate"},
   197  		func(label, value string) (string, string, error) {
   198  			switch label {
   199  			case "metadata.name":
   200  				return label, value, nil
   201  			default:
   202  				return "", "", fmt.Errorf("field label not supported: %s", label)
   203  			}
   204  		},
   205  	)
   206  	if err != nil {
   207  		return err
   208  	}
   209  
   210  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "GlobalReport"},
   211  		func(label, value string) (string, string, error) {
   212  			switch label {
   213  			case "metadata.name":
   214  				return label, value, nil
   215  			default:
   216  				return "", "", fmt.Errorf("field label not supported: %s", label)
   217  			}
   218  		},
   219  	)
   220  	if err != nil {
   221  		return err
   222  	}
   223  
   224  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "GlobalReportType"},
   225  		func(label, value string) (string, string, error) {
   226  			switch label {
   227  			case "metadata.name":
   228  				return label, value, nil
   229  			default:
   230  				return "", "", fmt.Errorf("field label not supported: %s", label)
   231  			}
   232  		},
   233  	)
   234  	if err != nil {
   235  		return err
   236  	}
   237  
   238  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "GlobalThreatFeed"},
   239  		func(label, value string) (string, string, error) {
   240  			switch label {
   241  			case "metadata.name":
   242  				return label, value, nil
   243  			default:
   244  				return "", "", fmt.Errorf("field label not supported: %s", label)
   245  			}
   246  		},
   247  	)
   248  	if err != nil {
   249  		return err
   250  	}
   251  
   252  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "LicenseKey"},
   253  		func(label, value string) (string, string, error) {
   254  			switch label {
   255  			case "metadata.name":
   256  				return label, value, nil
   257  			default:
   258  				return "", "", fmt.Errorf("field label not supported: %s", label)
   259  			}
   260  		},
   261  	)
   262  	if err != nil {
   263  		return err
   264  	}
   265  
   266  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "ManagedCluster"},
   267  		func(label, value string) (string, string, error) {
   268  			switch label {
   269  			case "metadata.name":
   270  				return label, value, nil
   271  			default:
   272  				return "", "", fmt.Errorf("field label not supported: %s", label)
   273  			}
   274  		},
   275  	)
   276  	if err != nil {
   277  		return err
   278  	}
   279  
   280  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "PacketCapture"},
   281  		func(label, value string) (string, string, error) {
   282  			switch label {
   283  			case "metadata.name":
   284  				return label, value, nil
   285  			default:
   286  				return "", "", fmt.Errorf("field label not supported: %s", label)
   287  			}
   288  		},
   289  	)
   290  	if err != nil {
   291  		return err
   292  	}
   293  
   294  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "PolicyRecommendation"},
   295  		func(label, value string) (string, string, error) {
   296  			switch label {
   297  			case "metadata.name":
   298  				return label, value, nil
   299  			default:
   300  				return "", "", fmt.Errorf("field label not supported: %s", label)
   301  			}
   302  		},
   303  	)
   304  	if err != nil {
   305  		return err
   306  	}
   307  
   308  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "PolicyRecommendationScope"},
   309  		func(label, value string) (string, string, error) {
   310  			switch label {
   311  			case "metadata.name":
   312  				return label, value, nil
   313  			default:
   314  				return "", "", fmt.Errorf("field label not supported: %s", label)
   315  			}
   316  		},
   317  	)
   318  	if err != nil {
   319  		return err
   320  	}
   321  
   322  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "RemoteClusterConfiguration"},
   323  		func(label, value string) (string, string, error) {
   324  			switch label {
   325  			case "metadata.name":
   326  				return label, value, nil
   327  			default:
   328  				return "", "", fmt.Errorf("field label not supported: %s", label)
   329  			}
   330  		},
   331  	)
   332  	if err != nil {
   333  		return err
   334  	}
   335  
   336  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "StagedGlobalNetworkPolicy"},
   337  		func(label, value string) (string, string, error) {
   338  			switch label {
   339  			case "spec.tier", "metadata.name":
   340  				return label, value, nil
   341  			default:
   342  				return "", "", fmt.Errorf("field label not supported: %s", label)
   343  			}
   344  		},
   345  	)
   346  	if err != nil {
   347  		return err
   348  	}
   349  
   350  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "StagedKubernetesNetworkPolicy"},
   351  		func(label, value string) (string, string, error) {
   352  			switch label {
   353  			case "metadata.name":
   354  				return label, value, nil
   355  			default:
   356  				return "", "", fmt.Errorf("field label not supported: %s", label)
   357  			}
   358  		},
   359  	)
   360  	if err != nil {
   361  		return err
   362  	}
   363  
   364  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "StagedNetworkPolicy"},
   365  		func(label, value string) (string, string, error) {
   366  			switch label {
   367  			case "spec.tier", "metadata.name", "metadata.namespace":
   368  				return label, value, nil
   369  			default:
   370  				return "", "", fmt.Errorf("field label not supported: %s", label)
   371  			}
   372  		},
   373  	)
   374  	if err != nil {
   375  		return err
   376  	}
   377  
   378  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "Tier"},
   379  		func(label, value string) (string, string, error) {
   380  			switch label {
   381  			case "metadata.name":
   382  				return label, value, nil
   383  			default:
   384  				return "", "", fmt.Errorf("field label not supported: %s", label)
   385  			}
   386  		},
   387  	)
   388  	if err != nil {
   389  		return err
   390  	}
   391  
   392  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "UISettingsGroup"},
   393  		func(label, value string) (string, string, error) {
   394  			switch label {
   395  			case "metadata.name":
   396  				return label, value, nil
   397  			default:
   398  				return "", "", fmt.Errorf("field label not supported: %s", label)
   399  			}
   400  		},
   401  	)
   402  	if err != nil {
   403  		return err
   404  	}
   405  
   406  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "UISettings"},
   407  		func(label, value string) (string, string, error) {
   408  			switch label {
   409  			case "spec.group", "metadata.name":
   410  				return label, value, nil
   411  			default:
   412  				return "", "", fmt.Errorf("field label not supported: %s", label)
   413  			}
   414  		},
   415  	)
   416  	if err != nil {
   417  		return err
   418  	}
   419  
   420  	err = scheme.AddFieldLabelConversionFunc(schema.GroupVersionKind{Group: "projectcalico.org", Version: "v3", Kind: "AlertException"},
   421  		func(label, value string) (string, string, error) {
   422  			switch label {
   423  			case "metadata.name":
   424  				return label, value, nil
   425  			default:
   426  				return "", "", fmt.Errorf("field label not supported: %s", label)
   427  			}
   428  		},
   429  	)
   430  	if err != nil {
   431  		return err
   432  	}
   433  
   434  	return nil
   435  }