diff --git a/cmd/machine-api-migration/main.go b/cmd/machine-api-migration/main.go index d9f3bcbe6..c127ae6e0 100644 --- a/cmd/machine-api-migration/main.go +++ b/cmd/machine-api-migration/main.go @@ -21,6 +21,7 @@ import ( "os" "time" + metal3v1 "github.com/metal3-io/cluster-api-provider-metal3/api/v1beta1" configv1 "github.com/openshift/api/config/v1" mapiv1alpha1 "github.com/openshift/api/machine/v1alpha1" mapiv1beta1 "github.com/openshift/api/machine/v1beta1" @@ -68,6 +69,7 @@ func initScheme(scheme *runtime.Scheme) { utilruntime.Must(mapiv1beta1.Install(scheme)) utilruntime.Must(configv1.Install(scheme)) utilruntime.Must(awsv1.AddToScheme(scheme)) + utilruntime.Must(metal3v1.AddToScheme(scheme)) utilruntime.Must(openstackv1.AddToScheme(scheme)) utilruntime.Must(clusterv1.AddToScheme(scheme)) } @@ -196,7 +198,7 @@ func main() { // Currently we only plan to support AWS, so all others are a noop until they're implemented. switch provider { - case configv1.AWSPlatformType, configv1.OpenStackPlatformType: + case configv1.AWSPlatformType, configv1.BareMetalPlatformType, configv1.OpenStackPlatformType: klog.Infof("MachineAPIMigration: starting %s controllers", provider) default: diff --git a/e2e/go.mod b/e2e/go.mod index f0379dbe7..2a9797ca7 100644 --- a/e2e/go.mod +++ b/e2e/go.mod @@ -11,7 +11,7 @@ require ( github.com/onsi/gomega v1.38.0 github.com/openshift/api v0.0.0-20250731015415-ed654edbd7c6 github.com/openshift/cluster-api-actuator-pkg v0.0.0-20250729202911-167220318f40 - github.com/openshift/cluster-api-provider-baremetal v0.0.0-20250619124612-fb678fec5f7e + github.com/openshift/cluster-api-provider-baremetal v0.0.0-20250930083252-4e6a729c5ac8 k8s.io/api v0.33.3 k8s.io/apimachinery v0.33.3 k8s.io/client-go v0.33.3 diff --git a/e2e/go.sum b/e2e/go.sum index d7f123600..f4fe2b4d7 100644 --- a/e2e/go.sum +++ b/e2e/go.sum @@ -188,8 +188,8 @@ github.com/openshift/cluster-api-actuator-pkg v0.0.0-20250729202911-167220318f40 github.com/openshift/cluster-api-actuator-pkg v0.0.0-20250729202911-167220318f40/go.mod h1:gYQH3j+az83vG5IuZZohskiBmqLDw7dzhmn1x5Zq5pg= github.com/openshift/cluster-api-actuator-pkg/testutils v0.0.0-20250821122144-fd0936342469 h1:2Nb7w9xhgZg/ahmvrG4Y/+6Tp/Gfj7KGZZKAupKah8g= github.com/openshift/cluster-api-actuator-pkg/testutils v0.0.0-20250821122144-fd0936342469/go.mod h1:9+FWWWLkVrnBo1eYhA/0Ehlq5JMgIAHtcB0IF+qV1AA= -github.com/openshift/cluster-api-provider-baremetal v0.0.0-20250619124612-fb678fec5f7e h1:kW+b46Js8hn2oZwsuaYzdX7KAH/Aa9Wy3v1iHGJ3l4o= -github.com/openshift/cluster-api-provider-baremetal v0.0.0-20250619124612-fb678fec5f7e/go.mod h1:xMvpaHoSmJ48WvX970zF82PPD9Wj8cMuZBFH6UxnvbE= +github.com/openshift/cluster-api-provider-baremetal v0.0.0-20250930083252-4e6a729c5ac8 h1:QMC+nokAQLdS8XK488fRlTNgbhuWytNUyRm2w0gsEBc= +github.com/openshift/cluster-api-provider-baremetal v0.0.0-20250930083252-4e6a729c5ac8/go.mod h1:xMvpaHoSmJ48WvX970zF82PPD9Wj8cMuZBFH6UxnvbE= github.com/openshift/cluster-autoscaler-operator v0.0.1-0.20250702183526-4eb64d553940 h1:NCCRJ7JfGLpRu4IQSV7Qw9VoAdIgF1BiwospisP06a8= github.com/openshift/cluster-autoscaler-operator v0.0.1-0.20250702183526-4eb64d553940/go.mod h1:zCklcJwbnaNx46KvR38Rh86uZdow5gvub4ATcNDopTM= github.com/openshift/library-go v0.0.0-20250729191057-91376e1b394e h1:xYT+P++PSc9G+Y47pIcU9fm8IDV/tg6tMi3i+0m23pU= diff --git a/go.mod b/go.mod index fca85a6aa..f2d268232 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/openshift/api v0.0.0-20250731015415-ed654edbd7c6 github.com/openshift/client-go v0.0.0-20250710075018-396b36f983ee github.com/openshift/cluster-api-actuator-pkg/testutils v0.0.0-20250821122144-fd0936342469 + github.com/openshift/cluster-api-provider-baremetal v0.0.0-20250930083252-4e6a729c5ac8 github.com/openshift/library-go v0.0.0-20250729191057-91376e1b394e github.com/pkg/errors v0.9.1 github.com/spf13/pflag v1.0.6 @@ -303,3 +304,5 @@ require ( sigs.k8s.io/kube-storage-version-migrator v0.0.6-0.20230721195810-5c8923c5ff96 // indirect sigs.k8s.io/structured-merge-diff/v4 v4.7.0 // indirect ) + +replace github.com/metal3-io/cluster-api-provider-metal3/api => github.com/metal3-io/cluster-api-provider-metal3/api v1.10.1 diff --git a/go.sum b/go.sum index 74bd4937d..9da38680d 100644 --- a/go.sum +++ b/go.sum @@ -425,6 +425,8 @@ github.com/openshift/client-go v0.0.0-20250710075018-396b36f983ee h1:tOtrrxfDEW8 github.com/openshift/client-go v0.0.0-20250710075018-396b36f983ee/go.mod h1:zhRiYyNMk89llof2qEuGPWPD+joQPhCRUc2IK0SB510= github.com/openshift/cluster-api-actuator-pkg/testutils v0.0.0-20250821122144-fd0936342469 h1:2Nb7w9xhgZg/ahmvrG4Y/+6Tp/Gfj7KGZZKAupKah8g= github.com/openshift/cluster-api-actuator-pkg/testutils v0.0.0-20250821122144-fd0936342469/go.mod h1:9+FWWWLkVrnBo1eYhA/0Ehlq5JMgIAHtcB0IF+qV1AA= +github.com/openshift/cluster-api-provider-baremetal v0.0.0-20250930083252-4e6a729c5ac8 h1:QMC+nokAQLdS8XK488fRlTNgbhuWytNUyRm2w0gsEBc= +github.com/openshift/cluster-api-provider-baremetal v0.0.0-20250930083252-4e6a729c5ac8/go.mod h1:xMvpaHoSmJ48WvX970zF82PPD9Wj8cMuZBFH6UxnvbE= github.com/openshift/library-go v0.0.0-20250729191057-91376e1b394e h1:xYT+P++PSc9G+Y47pIcU9fm8IDV/tg6tMi3i+0m23pU= github.com/openshift/library-go v0.0.0-20250729191057-91376e1b394e/go.mod h1:tptKNust9MdRI0p90DoBSPHIrBa9oh+Rok59tF0vT8c= github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw= diff --git a/go.work.sum b/go.work.sum index d5f7f84a3..e4d5cb2b3 100644 --- a/go.work.sum +++ b/go.work.sum @@ -624,7 +624,6 @@ github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJS github.com/jessevdk/go-flags v1.6.1/go.mod h1:Mk8T1hIAWpOiJiHa9rJASDK2UGWji0EuPGBnNLMooyc= github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af/go.mod h1:HEWGJkRDzjJY2sqdDwxccsGicWEf9BQOZsq2tV+xzM0= github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= -github.com/jmespath/go-jmespath v0.4.1-0.20220621161143-b0104c826a24/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ= github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= @@ -633,7 +632,6 @@ github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= -github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= @@ -644,10 +642,7 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kyoh86/exportloopref v0.1.11/go.mod h1:qkV4UF1zGl6EkF1ox8L5t9SwyeBAZ3qLMd6up458uqA= github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= @@ -790,7 +785,6 @@ github.com/sanathkr/go-yaml v0.0.0-20170819195128-ed9d249f429b/go.mod h1:8458kAa github.com/sanathkr/yaml v0.0.0-20170819201035-0056894fa522/go.mod h1:tQTYKOQgxoH3v6dEmdHiz4JG+nbxWwM5fgPQUpSZqVQ= github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY= github.com/seccomp/libseccomp-golang v0.10.0/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg= -github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I= github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c/go.mod h1:/PevMnwAxekIXwN8qQyfc5gl2NlkB3CQlkizAbOkeBs= github.com/shirou/gopsutil/v4 v4.25.2/go.mod h1:34gBYJzyqCDT11b6bMHP0XCvWeU3J61XRT7a2EmCRTA= github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME= @@ -1384,7 +1378,6 @@ google.golang.org/protobuf v1.36.3/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojt google.golang.org/protobuf v1.36.4/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= @@ -1472,11 +1465,8 @@ sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h6 sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3/go.mod h1:18nIHnGi6636UCz6m8i4DhaJ65T6EruyzmoQqI2BVDo= sigs.k8s.io/kind v0.27.0/go.mod h1:RZVFmy6qcwlSWwp6xeIUv7kXCPF3i8MXsEXxW/J+gJY= sigs.k8s.io/kind v0.29.0/go.mod h1:ldWQisw2NYyM6k64o/tkZng/1qQW7OlzcN5a8geJX3o= -sigs.k8s.io/kustomize/api v0.19.0 h1:F+2HB2mU1MSiR9Hp1NEgoU2q9ItNOaBJl0I4Dlus5SQ= -sigs.k8s.io/kustomize/api v0.19.0/go.mod h1:/BbwnivGVcBh1r+8m3tH1VNxJmHSk1PzP5fkP6lbL1o= +sigs.k8s.io/kustomize/cmd/config v0.19.0/go.mod h1:29Vvdl26PidPLUDi7nfjYa/I0wHBkwCZp15Nlcc4y98= sigs.k8s.io/kustomize/kustomize/v5 v5.6.0/go.mod h1:XuuZiQF7WdcvZzEYyNww9A0p3LazCKeJmCjeycN8e1I= -sigs.k8s.io/kustomize/kyaml v0.19.0 h1:RFge5qsO1uHhwJsu3ipV7RNolC7Uozc0jUBC/61XSlA= -sigs.k8s.io/kustomize/kyaml v0.19.0/go.mod h1:FeKD5jEOH+FbZPpqUghBP8mrLjJ3+zD3/rf9NNu1cwY= sigs.k8s.io/structured-merge-diff/v4 v4.4.2/go.mod h1:N8f93tFZh9U6vpxwRArLiikrE5/2tiu1w1AGfACIGE4= sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc= software.sslmate.com/src/go-pkcs12 v0.5.0/go.mod h1:Qiz0EyvDRJjjxGyUQa2cCNZn/wMyzrRJ/qcDXOQazLI= diff --git a/hack/tools/go.mod b/hack/tools/go.mod index f345adc5d..f171ca982 100644 --- a/hack/tools/go.mod +++ b/hack/tools/go.mod @@ -8,7 +8,7 @@ require ( github.com/golangci/golangci-lint v1.64.8 github.com/jstemmer/go-junit-report v0.9.1 github.com/onsi/ginkgo/v2 v2.23.4 - sigs.k8s.io/kustomize/kustomize/v5 v5.4.1 + sigs.k8s.io/kustomize/kustomize/v5 v5.6.0 ) require ( @@ -213,7 +213,7 @@ require ( mvdan.cc/unparam v0.0.0-20240528143540-8a5130ca722f // indirect sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect sigs.k8s.io/kustomize/api v0.19.0 // indirect - sigs.k8s.io/kustomize/cmd/config v0.14.0 // indirect + sigs.k8s.io/kustomize/cmd/config v0.19.0 // indirect sigs.k8s.io/kustomize/kyaml v0.19.0 // indirect sigs.k8s.io/yaml v1.4.0 // indirect ) diff --git a/hack/tools/go.sum b/hack/tools/go.sum index 590cd936f..bf99df159 100644 --- a/hack/tools/go.sum +++ b/hack/tools/go.sum @@ -599,10 +599,10 @@ sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 h1:gBQPwqORJ8d8/YNZWEjoZs7np sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8/go.mod h1:mdzfpAEoE6DHQEN0uh9ZbOCuHbLK5wOm7dK4ctXE9Tg= sigs.k8s.io/kustomize/api v0.19.0 h1:F+2HB2mU1MSiR9Hp1NEgoU2q9ItNOaBJl0I4Dlus5SQ= sigs.k8s.io/kustomize/api v0.19.0/go.mod h1:/BbwnivGVcBh1r+8m3tH1VNxJmHSk1PzP5fkP6lbL1o= -sigs.k8s.io/kustomize/cmd/config v0.14.0 h1:TMKWBtk0Ox1EUwafT9hncg7EFrvrBlqDDHXmBHm51U8= -sigs.k8s.io/kustomize/cmd/config v0.14.0/go.mod h1:J+ukok4u7k8esCzEhRTygTW7dzDKYbC0kCDkpBK3VfI= -sigs.k8s.io/kustomize/kustomize/v5 v5.4.1 h1:97qXYnSngAvY+gUAzMfjJ73ClKZAD+JrKE+7OIMWYhw= -sigs.k8s.io/kustomize/kustomize/v5 v5.4.1/go.mod h1:nKouayWQbaro85jrNbcA+tZ6x63wYYxGPgniA/fEmgQ= +sigs.k8s.io/kustomize/cmd/config v0.19.0 h1:D3uASwjHWHmNiEHu3pPJBJMBIsb+auFvHrHql3HAarU= +sigs.k8s.io/kustomize/cmd/config v0.19.0/go.mod h1:29Vvdl26PidPLUDi7nfjYa/I0wHBkwCZp15Nlcc4y98= +sigs.k8s.io/kustomize/kustomize/v5 v5.6.0 h1:MWtRRDWCwQEeW2rnJTqJMuV6Agy56P53SkbVoJpN7wA= +sigs.k8s.io/kustomize/kustomize/v5 v5.6.0/go.mod h1:XuuZiQF7WdcvZzEYyNww9A0p3LazCKeJmCjeycN8e1I= sigs.k8s.io/kustomize/kyaml v0.19.0 h1:RFge5qsO1uHhwJsu3ipV7RNolC7Uozc0jUBC/61XSlA= sigs.k8s.io/kustomize/kyaml v0.19.0/go.mod h1:FeKD5jEOH+FbZPpqUghBP8mrLjJ3+zD3/rf9NNu1cwY= sigs.k8s.io/randfill v1.0.0 h1:JfjMILfT8A6RbawdsK2JXGBR5AQVfd+9TbzrlneTyrU= diff --git a/pkg/controllers/common.go b/pkg/controllers/common.go index 837c77fcb..4d7e3e3f8 100644 --- a/pkg/controllers/common.go +++ b/pkg/controllers/common.go @@ -19,6 +19,7 @@ import ( "errors" "fmt" + metal3v1 "github.com/metal3-io/cluster-api-provider-metal3/api/v1beta1" configv1 "github.com/openshift/api/config/v1" awsv1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" ibmpowervsv1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2" @@ -42,6 +43,8 @@ func InitInfraMachineAndInfraClusterFromProvider(platform configv1.PlatformType) switch platform { case configv1.AWSPlatformType: return &awsv1.AWSMachine{}, &awsv1.AWSCluster{}, nil + case configv1.BareMetalPlatformType: + return &metal3v1.Metal3Machine{}, &metal3v1.Metal3Cluster{}, nil case configv1.OpenStackPlatformType: return &openstackv1.OpenStackMachine{}, &openstackv1.OpenStackCluster{}, nil case configv1.PowerVSPlatformType: @@ -59,6 +62,8 @@ func InitInfraMachineTemplateAndInfraClusterFromProvider(platform configv1.Platf switch platform { case configv1.AWSPlatformType: return &awsv1.AWSMachineTemplate{}, &awsv1.AWSCluster{}, nil + case configv1.BareMetalPlatformType: + return &metal3v1.Metal3MachineTemplate{}, &metal3v1.Metal3Cluster{}, nil case configv1.OpenStackPlatformType: return &openstackv1.OpenStackMachineTemplate{}, &openstackv1.OpenStackCluster{}, nil case configv1.PowerVSPlatformType: diff --git a/pkg/controllers/machinesetsync/machineset_sync_controller.go b/pkg/controllers/machinesetsync/machineset_sync_controller.go index d4654a75c..6e18986b3 100644 --- a/pkg/controllers/machinesetsync/machineset_sync_controller.go +++ b/pkg/controllers/machinesetsync/machineset_sync_controller.go @@ -40,6 +40,7 @@ import ( utilerrors "k8s.io/apimachinery/pkg/util/errors" "github.com/go-test/deep" + metal3v1 "github.com/metal3-io/cluster-api-provider-metal3/api/v1beta1" machinev1applyconfigs "github.com/openshift/client-go/machine/applyconfigurations/machine/v1beta1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" @@ -75,6 +76,9 @@ var ( // errAssertingCAPIAWSMachineTemplate is returned when we encounter an issue asserting a client.Object into a AWSMachineTemplate. errAssertingCAPIAWSMachineTemplate = errors.New("error asserting the CAPI AWSMachineTemplate object") + // errAssertingCAPIMetal3MachineTemplate is returned when we encounter an issue asserting a client.Object into a Metal3MachineTemplate. + errAssertingCAPIMetal3MachineTemplate = errors.New("error asserting the CAPI Metal3MachineTemplate object") + // errAssertingCAPIPowerVSMachineTemplate is returned when we encounter an issue asserting a client.Object into a IBMPowerVSMachineTemplate. errAssertingCAPIIBMPowerVSMachineTemplate = errors.New("error asserting the CAPI IBMPowerVSMachineTemplate object") @@ -665,6 +669,20 @@ func (r *MachineSetSyncReconciler) convertCAPIToMAPIMachineSet(capiMachineSet *c return capi2mapi.FromMachineSetAndAWSMachineTemplateAndAWSCluster( //nolint: wrapcheck capiMachineSet, machineTemplate, cluster, ).ToMachineSet() + case configv1.BareMetalPlatformType: + machineTemplate, ok := infraMachineTemplate.(*metal3v1.Metal3MachineTemplate) + if !ok { + return nil, nil, fmt.Errorf("%w, expected Metal3MachineTemplate, got %T", errUnexpectedInfraMachineTemplateType, infraMachineTemplate) + } + + cluster, ok := infraCluster.(*metal3v1.Metal3Cluster) + if !ok { + return nil, nil, fmt.Errorf("%w, expected Metal3Cluster, got %T", errUnexpectedInfraClusterType, infraCluster) + } + + return capi2mapi.FromMachineSetAndMetal3MachineTemplateAndMetal3Cluster( //nolint: wrapcheck + capiMachineSet, machineTemplate, cluster, + ).ToMachineSet() case configv1.OpenStackPlatformType: machineTemplate, ok := infraMachineTemplate.(*openstackv1.OpenStackMachineTemplate) if !ok { @@ -703,6 +721,8 @@ func (r *MachineSetSyncReconciler) convertMAPIToCAPIMachineSet(mapiMachineSet *m switch r.Platform { case configv1.AWSPlatformType: return mapi2capi.FromAWSMachineSetAndInfra(mapiMachineSet, r.Infra).ToMachineSetAndMachineTemplate() //nolint:wrapcheck + case configv1.BareMetalPlatformType: + return mapi2capi.FromMetal3MachineSetAndInfra(mapiMachineSet, r.Infra).ToMachineSetAndMachineTemplate() //nolint:wrapcheck case configv1.OpenStackPlatformType: return mapi2capi.FromOpenStackMachineSetAndInfra(mapiMachineSet, r.Infra).ToMachineSetAndMachineTemplate() //nolint:wrapcheck case configv1.PowerVSPlatformType: @@ -1302,6 +1322,8 @@ func initInfraMachineTemplateListAndInfraClusterListFromProvider(platform config switch platform { case configv1.AWSPlatformType: return &awsv1.AWSMachineTemplateList{}, &awsv1.AWSClusterList{}, nil + case configv1.BareMetalPlatformType: + return &metal3v1.Metal3MachineTemplateList{}, &metal3v1.Metal3ClusterList{}, nil case configv1.OpenStackPlatformType: return &openstackv1.OpenStackMachineTemplateList{}, &openstackv1.OpenStackClusterList{}, nil case configv1.PowerVSPlatformType: @@ -1313,7 +1335,7 @@ func initInfraMachineTemplateListAndInfraClusterListFromProvider(platform config // compareCAPIInfraMachineTemplates compares CAPI infra machine templates a and b, and returns a list of differences, or none if there are none. // -//nolint:funlen +//nolint:funlen,gocognit,cyclop func compareCAPIInfraMachineTemplates(platform configv1.PlatformType, infraMachineTemplate1, infraMachineTemplate2 client.Object) (map[string]any, error) { switch platform { case configv1.AWSPlatformType: @@ -1339,6 +1361,30 @@ func compareCAPIInfraMachineTemplates(platform configv1.PlatformType, infraMachi // TODO: Evaluate if we want to add status comparison if needed in the future (e.g. for scale from zero capacity). + return diff, nil + case configv1.BareMetalPlatformType: + typedInfraMachineTemplate1, ok := infraMachineTemplate1.(*metal3v1.Metal3MachineTemplate) + if !ok { + return nil, errAssertingCAPIMetal3MachineTemplate + } + + typedinfraMachineTemplate2, ok := infraMachineTemplate2.(*metal3v1.Metal3MachineTemplate) + if !ok { + return nil, errAssertingCAPIMetal3MachineTemplate + } + + diff := make(map[string]any) + + if diffSpec := deep.Equal(typedInfraMachineTemplate1.Spec, typedinfraMachineTemplate2.Spec); len(diffSpec) > 0 { + diff[".spec"] = diffSpec + } + + if diffObjectMeta := util.ObjectMetaEqual(typedInfraMachineTemplate1.ObjectMeta, typedinfraMachineTemplate2.ObjectMeta); len(diffObjectMeta) > 0 { + diff[".metadata"] = diffObjectMeta + } + + // TODO: Evaluate if we want to add status comparison if needed in the future (e.g. for scale from zero capacity). + return diff, nil case configv1.OpenStackPlatformType: typedInfraMachineTemplate1, ok := infraMachineTemplate1.(*openstackv1.OpenStackMachineTemplate) diff --git a/pkg/controllers/machinesync/machine_sync_controller.go b/pkg/controllers/machinesync/machine_sync_controller.go index 39ae65243..5f19c458c 100644 --- a/pkg/controllers/machinesync/machine_sync_controller.go +++ b/pkg/controllers/machinesync/machine_sync_controller.go @@ -37,6 +37,7 @@ import ( "github.com/openshift/cluster-capi-operator/pkg/util" "github.com/go-test/deep" + metal3v1 "github.com/metal3-io/cluster-api-provider-metal3/api/v1beta1" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -96,6 +97,9 @@ var ( // errAssertingCAPIAWSMachine is returned when we encounter an issue asserting a client.Object into an AWSMachine. errAssertingCAPIAWSMachine = errors.New("error asserting the Cluster API AWSMachine object") + // errAssertingCAPIMetal3Machine is returned when we encounter an issue asserting a client.Object into a Metal3Machine. + errAssertingCAPIMetal3Machine = errors.New("error asserting the Cluster API Metal3Machine object") + // errAssertingCAPIOpenStackMachine is returned when we encounter an issue asserting a client.Object into an OpenStackVSMachine. errAssertingCAPIOpenStackMachine = errors.New("error asserting the Cluster API OpenStackMachine object") @@ -582,6 +586,8 @@ func (r *MachineSyncReconciler) convertMAPIToCAPIMachine(mapiMachine *mapiv1beta switch r.Platform { case configv1.AWSPlatformType: return mapi2capi.FromAWSMachineAndInfra(mapiMachine, r.Infra).ToMachineAndInfrastructureMachine() //nolint:wrapcheck + case configv1.BareMetalPlatformType: + return mapi2capi.FromMetal3MachineAndInfra(mapiMachine, r.Infra).ToMachineAndInfrastructureMachine() //nolint:wrapcheck case configv1.OpenStackPlatformType: return mapi2capi.FromOpenStackMachineAndInfra(mapiMachine, r.Infra).ToMachineAndInfrastructureMachine() //nolint:wrapcheck case configv1.PowerVSPlatformType: @@ -605,6 +611,18 @@ func (r *MachineSyncReconciler) convertCAPIToMAPIMachine(capiMachine *clusterv1. } return capi2mapi.FromMachineAndAWSMachineAndAWSCluster(capiMachine, awsMachine, awsCluster).ToMachine() //nolint:wrapcheck + case configv1.BareMetalPlatformType: + metal3Machine, ok := infraMachine.(*metal3v1.Metal3Machine) + if !ok { + return nil, nil, fmt.Errorf("%w, expected Metal3Machine, got %T", errUnexpectedInfraMachineType, infraMachine) + } + + metal3Cluster, ok := infraCluster.(*metal3v1.Metal3Cluster) + if !ok { + return nil, nil, fmt.Errorf("%w, expected Metal3Cluster, got %T", errUnexpectedInfraClusterType, infraCluster) + } + + return capi2mapi.FromMachineAndMetal3MachineAndMetal3Cluster(capiMachine, metal3Machine, metal3Cluster).ToMachine() //nolint:wrapcheck case configv1.OpenStackPlatformType: openStackMachine, ok := infraMachine.(*openstackv1.OpenStackMachine) if !ok { diff --git a/pkg/controllers/machinesync/machine_sync_mapi2capi_infrastructure.go b/pkg/controllers/machinesync/machine_sync_mapi2capi_infrastructure.go index 3edf96b28..1c54cc770 100644 --- a/pkg/controllers/machinesync/machine_sync_mapi2capi_infrastructure.go +++ b/pkg/controllers/machinesync/machine_sync_mapi2capi_infrastructure.go @@ -21,6 +21,7 @@ import ( "fmt" "github.com/go-test/deep" + metal3v1 "github.com/metal3-io/cluster-api-provider-metal3/api/v1beta1" configv1 "github.com/openshift/api/config/v1" mapiv1beta1 "github.com/openshift/api/machine/v1beta1" "github.com/openshift/cluster-capi-operator/pkg/util" @@ -247,7 +248,7 @@ func (r *MachineSyncReconciler) ensureCAPIInfraMachineDeleted(ctx context.Contex // compareCAPIInfraMachines compares Cluster API infra machines a and b, and returns a list of differences, or none if there are none. // -//nolint:funlen,gocognit +//nolint:funlen,gocognit,cyclop func compareCAPIInfraMachines(platform configv1.PlatformType, infraMachine1, infraMachine2 client.Object) (map[string]any, error) { diff := make(map[string]any) @@ -293,6 +294,28 @@ func compareCAPIInfraMachines(platform configv1.PlatformType, infraMachine1, inf diff[".metadata"] = diffMetadata } + if diffStatus := deep.Equal(typedInfraMachine1.Status, typedinfraMachine2.Status); len(diffStatus) > 0 { + diff[".status"] = diffStatus + } + case configv1.BareMetalPlatformType: + typedInfraMachine1, ok := infraMachine1.(*metal3v1.Metal3Machine) + if !ok { + return nil, errAssertingCAPIMetal3Machine + } + + typedinfraMachine2, ok := infraMachine2.(*metal3v1.Metal3Machine) + if !ok { + return nil, errAssertingCAPIMetal3Machine + } + + if diffSpec := deep.Equal(typedInfraMachine1.Spec, typedinfraMachine2.Spec); len(diffSpec) > 0 { + diff[".spec"] = diffSpec + } + + if diffMetadata := util.ObjectMetaEqual(typedInfraMachine1.ObjectMeta, typedinfraMachine2.ObjectMeta); len(diffMetadata) > 0 { + diff[".metadata"] = diffMetadata + } + if diffStatus := deep.Equal(typedInfraMachine1.Status, typedinfraMachine2.Status); len(diffStatus) > 0 { diff[".status"] = diffStatus } diff --git a/pkg/conversion/capi2mapi/metal3.go b/pkg/conversion/capi2mapi/metal3.go new file mode 100644 index 000000000..c0be0ae06 --- /dev/null +++ b/pkg/conversion/capi2mapi/metal3.go @@ -0,0 +1,285 @@ +/* +Copyright 2025 Red Hat, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package capi2mapi + +import ( + "encoding/json" + "errors" + "fmt" + + metal3v1 "github.com/metal3-io/cluster-api-provider-metal3/api/v1beta1" + baremetalv1 "github.com/openshift/cluster-api-provider-baremetal/pkg/apis/baremetal/v1alpha1" + + mapiv1beta1 "github.com/openshift/api/machine/v1beta1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + utilerrors "k8s.io/apimachinery/pkg/util/errors" + "k8s.io/apimachinery/pkg/util/validation/field" + "k8s.io/utils/ptr" + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" +) + +var ( + errCAPIMachineMetal3MachineMetal3ClusterCannotBeNil = errors.New("provided Machine, Metal3Machine and Metal3Cluster can not be nil") + errCAPIMachineSetMetal3MachineTemplateMetal3ClusterCannotBeNil = errors.New("provided MachineSet, Metal3MachineTemplate and Metal3Cluster can not be nil") +) + +// machineAndMetal3MachineAndMetal3Cluster stores the details of a Cluster API Machine and Metal3Machine and Metal3Cluster. +type machineAndMetal3MachineAndMetal3Cluster struct { + machine *clusterv1.Machine + metal3Machine *metal3v1.Metal3Machine + metal3Cluster *metal3v1.Metal3Cluster +} + +// machineSetAndMetal3MachineTemplateAndMetal3Cluster stores the details of a Cluster API MachineSet and Metal3MachineTemplate and Metal3Cluster. +type machineSetAndMetal3MachineTemplateAndMetal3Cluster struct { + machineSet *clusterv1.MachineSet + template *metal3v1.Metal3MachineTemplate + *machineAndMetal3MachineAndMetal3Cluster +} + +// FromMachineAndMetal3MachineAndMetal3Cluster wraps a CAPI Machine and CAPM3 Metal3Machine and CAPM3 Metal3Cluster into a capi2mapi MachineAndInfrastructureMachine. +func FromMachineAndMetal3MachineAndMetal3Cluster(m *clusterv1.Machine, mm *metal3v1.Metal3Machine, mc *metal3v1.Metal3Cluster) MachineAndInfrastructureMachine { + return &machineAndMetal3MachineAndMetal3Cluster{machine: m, metal3Machine: mm, metal3Cluster: mc} +} + +// FromMachineSetAndMetal3MachineTemplateAndMetal3Cluster wraps a CAPI MachineSet and CAPM3 Metal3MachineTemplate and CAPM3 Metal3Cluster into a capi2mapi MachineSetAndMachineTemplate. +func FromMachineSetAndMetal3MachineTemplateAndMetal3Cluster(ms *clusterv1.MachineSet, mts *metal3v1.Metal3MachineTemplate, mc *metal3v1.Metal3Cluster) MachineSetAndMachineTemplate { + return &machineSetAndMetal3MachineTemplateAndMetal3Cluster{ + machineSet: ms, + template: mts, + machineAndMetal3MachineAndMetal3Cluster: &machineAndMetal3MachineAndMetal3Cluster{ + machine: &clusterv1.Machine{ + ObjectMeta: metav1.ObjectMeta{ + Labels: ms.Spec.Template.ObjectMeta.Labels, + Annotations: ms.Spec.Template.ObjectMeta.Annotations, + }, + Spec: ms.Spec.Template.Spec, + }, + metal3Machine: &metal3v1.Metal3Machine{ + Spec: mts.Spec.Template.Spec, + }, + metal3Cluster: mc, + }, + } +} + +// toProviderSpec converts a capi2mapi MachineAndMetal3MachineAndMetal3Cluster into a MAPI BareMetalMachineProviderSpec. +// +//nolint:unparam +func (m machineAndMetal3MachineAndMetal3Cluster) toProviderSpec() (*baremetalv1.BareMetalMachineProviderSpec, []string, field.ErrorList) { + var ( + warnings []string + errors field.ErrorList + ) + + fldPath := field.NewPath("spec") + + mapbProviderConfig := baremetalv1.BareMetalMachineProviderSpec{ + TypeMeta: metav1.TypeMeta{ + Kind: "BareMetalMachineProviderSpec", + APIVersion: "baremetal.cluster.k8s.io/v1alpha1", + }, + // ObjectMeta - Only present because it's needed to form part of the runtime.RawExtension, not actually used by the provider. + Image: convertMetal3ImageToBareMetal(m.metal3Machine.Spec.Image), + CustomDeploy: convertMetal3CustomDeployToBareMetal(m.metal3Machine.Spec.CustomDeploy), + UserData: m.metal3Machine.Spec.UserData, + HostSelector: convertMetal3HostSelectorToBareMetal(m.metal3Machine.Spec.HostSelector), + } + + // Below this line are fields not used from the CAPI Metal3Machine. + + // ProviderID - Populated at a different level. + // DataTemplate - Not present in MAPI, not used in OpenShift. + // MetaData - Not present in MAPI, not used in OpenShift. + // NetworkData - Not present in MAPI, not used in OpenShift. + // AutomatedCleaningMode - Not present in MAPI, not used in OpenShift. + + // There are quite a few unsupported fields, so break them out for now. + errors = append(errors, handleUnsupportedMetal3MachineFields(fldPath, m.metal3Machine.Spec)...) + + if len(errors) > 0 { + return nil, warnings, errors + } + + return &mapbProviderConfig, warnings, nil +} + +// ToMachine converts a capi2mapi MachineAndMetal3Machine into a MAPI Machine. +func (m machineAndMetal3MachineAndMetal3Cluster) ToMachine() (*mapiv1beta1.Machine, []string, error) { + if m.machine == nil || m.metal3Machine == nil || m.metal3Cluster == nil { + return nil, nil, errCAPIMachineMetal3MachineMetal3ClusterCannotBeNil + } + + var ( + errors field.ErrorList + warnings []string + ) + + mapbSpec, warn, err := m.toProviderSpec() + if err != nil { + errors = append(errors, err...) + } + + bareMetalRawExt, errRaw := bareMetalRawExtensionFromProviderSpec(mapbSpec) + if errRaw != nil { + return nil, nil, fmt.Errorf("unable to convert BareMetal providerSpec to raw extension: %w", errRaw) + } + + warnings = append(warnings, warn...) + + mapiMachine, err := fromCAPIMachineToMAPIMachine(m.machine) + if err != nil { + errors = append(errors, err...) + } + + mapiMachine.Spec.ProviderSpec.Value = bareMetalRawExt + + if len(errors) > 0 { + return nil, warnings, errors.ToAggregate() + } + + return mapiMachine, warnings, nil +} + +// ToMachineSet converts a capi2mapi MachineAndMetal3MachineTemplate into a MAPI MachineSet. +// +//nolint:dupl +func (m machineSetAndMetal3MachineTemplateAndMetal3Cluster) ToMachineSet() (*mapiv1beta1.MachineSet, []string, error) { + if m.machineSet == nil || m.template == nil || m.metal3Cluster == nil || m.machineAndMetal3MachineAndMetal3Cluster == nil { + return nil, nil, errCAPIMachineSetMetal3MachineTemplateMetal3ClusterCannotBeNil + } + + var ( + errors []error + warnings []string + ) + + // Run the full ToMachine conversion so that we can check for + // any Machine level conversion errors in the spec translation. + mapiMachine, warn, err := m.ToMachine() + if err != nil { + errors = append(errors, err) + } + + warnings = append(warnings, warn...) + + mapiMachineSet, err := fromCAPIMachineSetToMAPIMachineSet(m.machineSet) + if err != nil { + errors = append(errors, err) + } + + if len(errors) > 0 { + return nil, warnings, utilerrors.NewAggregate(errors) + } + + mapiMachineSet.Spec.Template.Spec = mapiMachine.Spec + + // Copy the labels and annotations from the Machine to the template. + mapiMachineSet.Spec.Template.ObjectMeta.Annotations = mapiMachine.ObjectMeta.Annotations + mapiMachineSet.Spec.Template.ObjectMeta.Labels = mapiMachine.ObjectMeta.Labels + + return mapiMachineSet, warnings, nil +} + +//////// Conversion helpers + +func convertMetal3ImageToBareMetal(capiImage metal3v1.Image) baremetalv1.Image { + return baremetalv1.Image{ + URL: capiImage.URL, + Checksum: capiImage.Checksum, + // ChecksumType: Not present in MAPI, ignore. + // DiskFormat: Not present in MAPI, ignore. + } +} + +func convertMetal3CustomDeployToBareMetal(capiCustomDeploy *metal3v1.CustomDeploy) baremetalv1.CustomDeploy { + if capiCustomDeploy == nil { + return baremetalv1.CustomDeploy{} + } + + return baremetalv1.CustomDeploy{ + Method: capiCustomDeploy.Method, + } +} + +func convertMetal3HostSelectorToBareMetal(capiHostSelector metal3v1.HostSelector) baremetalv1.HostSelector { + return baremetalv1.HostSelector{ + MatchLabels: capiHostSelector.MatchLabels, + MatchExpressions: convertMetal3HostSelectorRequirementsToBareMetal(capiHostSelector.MatchExpressions), + } +} + +func convertMetal3HostSelectorRequirementsToBareMetal(capiRequirements []metal3v1.HostSelectorRequirement) []baremetalv1.HostSelectorRequirement { + if capiRequirements == nil { + return nil + } + + mapiRequirements := make([]baremetalv1.HostSelectorRequirement, 0, len(capiRequirements)) + for _, req := range capiRequirements { + mapiRequirements = append(mapiRequirements, baremetalv1.HostSelectorRequirement{ + Key: req.Key, + Operator: req.Operator, + Values: req.Values, + }) + } + + return mapiRequirements +} + +// handleUnsupportedMetal3MachineFields returns an error for every present field in the Metal3MachineSpec that +// we are currently, or indefinitely not supporting. +func handleUnsupportedMetal3MachineFields(fldPath *field.Path, spec metal3v1.Metal3MachineSpec) field.ErrorList { + errs := field.ErrorList{} + + if spec.DataTemplate != nil { + // DataTemplate is not present in MAPI and not used in OpenShift. + errs = append(errs, field.Invalid(fldPath.Child("dataTemplate"), spec.DataTemplate, "dataTemplate is not supported")) + } + + if spec.MetaData != nil { + // MetaData is not present in MAPI and not used in OpenShift. + errs = append(errs, field.Invalid(fldPath.Child("metaData"), spec.MetaData, "metaData is not supported")) + } + + if spec.NetworkData != nil { + // NetworkData is not present in MAPI and not used in OpenShift. + errs = append(errs, field.Invalid(fldPath.Child("networkData"), spec.NetworkData, "networkData is not supported")) + } + + if spec.AutomatedCleaningMode != nil && ptr.Deref(spec.AutomatedCleaningMode, "") != "" { + // AutomatedCleaningMode is not present in MAPI and not used in OpenShift. + errs = append(errs, field.Invalid(fldPath.Child("automatedCleaningMode"), spec.AutomatedCleaningMode, "automatedCleaningMode is not supported")) + } + + return errs +} + +// bareMetalRawExtensionFromProviderSpec marshals the machine provider spec. +func bareMetalRawExtensionFromProviderSpec(spec *baremetalv1.BareMetalMachineProviderSpec) (*runtime.RawExtension, error) { + if spec == nil { + return &runtime.RawExtension{}, nil + } + + rawBytes, err := json.Marshal(spec) + if err != nil { + return nil, fmt.Errorf("error marshalling providerSpec: %w", err) + } + + return &runtime.RawExtension{ + Raw: rawBytes, + }, nil +} diff --git a/pkg/conversion/mapi2capi/metal3.go b/pkg/conversion/mapi2capi/metal3.go new file mode 100644 index 000000000..1a11eb22a --- /dev/null +++ b/pkg/conversion/mapi2capi/metal3.go @@ -0,0 +1,327 @@ +/* +Copyright 2025 Red Hat, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package mapi2capi + +import ( + "fmt" + "reflect" + + configv1 "github.com/openshift/api/config/v1" + mapiv1beta1 "github.com/openshift/api/machine/v1beta1" + baremetalv1 "github.com/openshift/cluster-api-provider-baremetal/pkg/apis/baremetal/v1alpha1" + + metal3v1 "github.com/metal3-io/cluster-api-provider-metal3/api/v1beta1" + "github.com/openshift/cluster-capi-operator/pkg/util" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + utilerrors "k8s.io/apimachinery/pkg/util/errors" + "k8s.io/apimachinery/pkg/util/validation/field" + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/yaml" +) + +const ( + metal3MachineKind = "Metal3Machine" + metal3MachineTemplateKind = "Metal3MachineTemplate" +) + +// metal3MachineAndInfra stores the details of a Machine API BareMetalMachine and Infra. +type metal3MachineAndInfra struct { + machine *mapiv1beta1.Machine + infrastructure *configv1.Infrastructure +} + +// metal3MachineSetAndInfra stores the details of a Machine API BareMetalMachine set and Infra. +type metal3MachineSetAndInfra struct { + machineSet *mapiv1beta1.MachineSet + infrastructure *configv1.Infrastructure + *metal3MachineAndInfra +} + +// FromMetal3MachineAndInfra wraps a Machine API Machine for Metal3/BareMetal and the OCP Infrastructure object into a mapi2capi Metal3ProviderSpec. +func FromMetal3MachineAndInfra(m *mapiv1beta1.Machine, i *configv1.Infrastructure) Machine { + return &metal3MachineAndInfra{machine: m, infrastructure: i} +} + +// FromMetal3MachineSetAndInfra wraps a Machine API MachineSet for Metal3/BareMetal and the OCP Infrastructure object into a mapi2capi Metal3ProviderSpec. +func FromMetal3MachineSetAndInfra(m *mapiv1beta1.MachineSet, i *configv1.Infrastructure) MachineSet { + return &metal3MachineSetAndInfra{ + machineSet: m, + infrastructure: i, + metal3MachineAndInfra: &metal3MachineAndInfra{ + machine: &mapiv1beta1.Machine{ + Spec: m.Spec.Template.Spec, + }, + infrastructure: i, + }, + } +} + +// ToMachineAndInfrastructureMachine is used to generate a CAPI Machine and the corresponding InfrastructureMachine +// from the stored MAPI Machine and Infrastructure objects. +func (m *metal3MachineAndInfra) ToMachineAndInfrastructureMachine() (*clusterv1.Machine, client.Object, []string, error) { + capiMachine, capbMachine, warnings, errs := m.toMachineAndInfrastructureMachine() + + if len(errs) > 0 { + return nil, nil, warnings, errs.ToAggregate() + } + + return capiMachine, capbMachine, warnings, nil +} + +func (m *metal3MachineAndInfra) toMachineAndInfrastructureMachine() (*clusterv1.Machine, client.Object, []string, field.ErrorList) { + var ( + errs field.ErrorList + warnings []string + ) + + baremetalProviderConfig, err := BareMetalProviderSpecFromRawExtension(m.machine.Spec.ProviderSpec.Value) + if err != nil { + return nil, nil, nil, field.ErrorList{field.Invalid(field.NewPath("spec", "providerSpec", "value"), m.machine.Spec.ProviderSpec.Value, err.Error())} + } + + capbMachine, warn, machineErrs := m.toMetal3Machine(baremetalProviderConfig) + if machineErrs != nil { + errs = append(errs, machineErrs...) + } + + warnings = append(warnings, warn...) + + capiMachine, machineErrs := fromMAPIMachineToCAPIMachine(m.machine, metal3v1.GroupVersion.String(), metal3MachineKind) + if machineErrs != nil { + errs = append(errs, machineErrs...) + } + + // Extract and plug ProviderID on CAPM3, if the providerID is present on CAPI (instance has been provisioned). + if capiMachine.Spec.ProviderID != nil { + capbMachine.Spec.ProviderID = capiMachine.Spec.ProviderID + } + + // Plug into Core CAPI Machine fields that come from the MAPI ProviderConfig which belong here instead of the CAPI Metal3MachineTemplate. + if baremetalProviderConfig.UserData != nil && baremetalProviderConfig.UserData.Name != "" { + capiMachine.Spec.Bootstrap = clusterv1.Bootstrap{ + DataSecretName: &baremetalProviderConfig.UserData.Name, + } + } + + // Populate the CAPI Machine ClusterName from the OCP Infrastructure object. + if m.infrastructure == nil || m.infrastructure.Status.InfrastructureName == "" { + errs = append(errs, field.Invalid(field.NewPath("infrastructure", "status", "infrastructureName"), m.infrastructure.Status.InfrastructureName, "infrastructure cannot be nil and infrastructure.Status.InfrastructureName cannot be empty")) + } else { + capiMachine.Spec.ClusterName = m.infrastructure.Status.InfrastructureName + capiMachine.Labels[clusterv1.ClusterNameLabel] = m.infrastructure.Status.InfrastructureName + } + + // The InfraMachine should always have the same labels and annotations as the Machine. + // See https://github.com/kubernetes-sigs/cluster-api/blob/f88d7ae5155700c2cc367b31ddcc151c9ad579e4/internal/controllers/machineset/machineset_controller.go#L578-L579 + capiMachineAnnotations := capiMachine.GetAnnotations() + if len(capiMachineAnnotations) > 0 { + capbMachine.SetAnnotations(capiMachineAnnotations) + } + + capiMachineLabels := capiMachine.GetLabels() + if len(capiMachineLabels) > 0 { + capbMachine.SetLabels(capiMachineLabels) + } + + return capiMachine, capbMachine, warnings, errs +} + +// ToMachineSetAndMachineTemplate converts a mapi2capi Metal3MachineSetAndInfra into a CAPI MachineSet and CAPM3 Metal3MachineTemplate. +// +//nolint:dupl +func (m *metal3MachineSetAndInfra) ToMachineSetAndMachineTemplate() (*clusterv1.MachineSet, client.Object, []string, error) { + var ( + errs []error + warnings []string + ) + + capiMachine, capbMachineObj, warn, errList := m.toMachineAndInfrastructureMachine() + if errList != nil { + errs = append(errs, errList.ToAggregate().Errors()...) + } + + warnings = append(warnings, warn...) + + capbMachine, ok := capbMachineObj.(*metal3v1.Metal3Machine) + if !ok { + panic(fmt.Errorf("%w: %T", errUnexpectedObjectTypeForMachine, capbMachineObj)) + } + + capbMachineTemplate, err := metal3MachineToMetal3MachineTemplate(capbMachine, m.machineSet.Name, capiNamespace) + if err != nil { + errs = append(errs, err) + } + + capiMachineSet, machineSetErrs := fromMAPIMachineSetToCAPIMachineSet(m.machineSet) + if machineSetErrs != nil { + errs = append(errs, machineSetErrs.Errors()...) + } + + capiMachineSet.Spec.Template.Spec = capiMachine.Spec + + // We have to merge these two maps so that labels and annotations added to the template objectmeta are persisted + // along with the labels and annotations from the machine objectmeta. + capiMachineSet.Spec.Template.ObjectMeta.Labels = util.MergeMaps(capiMachineSet.Spec.Template.ObjectMeta.Labels, capiMachine.Labels) + capiMachineSet.Spec.Template.ObjectMeta.Annotations = util.MergeMaps(capiMachineSet.Spec.Template.ObjectMeta.Annotations, capiMachine.Annotations) + + // Override the reference so that it matches the Metal3MachineTemplate. + capiMachineSet.Spec.Template.Spec.InfrastructureRef.Kind = metal3MachineTemplateKind + capiMachineSet.Spec.Template.Spec.InfrastructureRef.Name = capbMachineTemplate.Name + + if m.infrastructure == nil || m.infrastructure.Status.InfrastructureName == "" { + errs = append(errs, field.Invalid(field.NewPath("infrastructure", "status", "infrastructureName"), m.infrastructure.Status.InfrastructureName, "infrastructure cannot be nil and infrastructure.Status.InfrastructureName cannot be empty")) + } else { + capiMachineSet.Spec.Template.Spec.ClusterName = m.infrastructure.Status.InfrastructureName + capiMachineSet.Spec.ClusterName = m.infrastructure.Status.InfrastructureName + capiMachineSet.Labels[clusterv1.ClusterNameLabel] = m.infrastructure.Status.InfrastructureName + } + + if len(errs) > 0 { + return nil, nil, warnings, utilerrors.NewAggregate(errs) + } + + return capiMachineSet, capbMachineTemplate, warnings, nil +} + +// toMetal3Machine implements the ProviderSpec conversion interface for the Metal3 provider, +// it converts BareMetalMachineProviderSpec to Metal3Machine. +// +//nolint:unparam +func (m *metal3MachineAndInfra) toMetal3Machine(providerSpec baremetalv1.BareMetalMachineProviderSpec) (*metal3v1.Metal3Machine, []string, field.ErrorList) { + fldPath := field.NewPath("spec", "providerSpec", "value") + + var ( + errs field.ErrorList + warnings []string + ) + + spec := metal3v1.Metal3MachineSpec{ + Image: convertBareMetalImageToMetal3(providerSpec.Image), + CustomDeploy: convertBareMetalCustomDeployToMetal3(providerSpec.CustomDeploy), + UserData: providerSpec.UserData, + HostSelector: convertBareMetalHostSelectorToMetal3(providerSpec.HostSelector), + // ProviderID: This is populated when this is called in higher level funcs (ToMachine(), ToMachineSet()). + // DataTemplate: Not present in MAPI, not used in OpenShift. + // MetaData: Not present in MAPI, not used in OpenShift. + // NetworkData: Not present in MAPI, not used in OpenShift. + // AutomatedCleaningMode: Not present in MAPI, not used in OpenShift. + } + + // Unused fields - Below this line are fields not used from the MAPI BareMetalMachineProviderConfig. + + // TypeMeta - Only for the purpose of the raw extension, not used for any functionality. + if !reflect.DeepEqual(providerSpec.ObjectMeta, metav1.ObjectMeta{}) { + // We don't support setting the object metadata in the provider spec. + // It's only present for the purpose of the raw extension and doesn't have any functionality. + errs = append(errs, field.Invalid(fldPath.Child("metadata"), providerSpec.ObjectMeta, "metadata is not supported")) + } + + return &metal3v1.Metal3Machine{ + TypeMeta: metav1.TypeMeta{ + APIVersion: metal3v1.GroupVersion.String(), + Kind: metal3MachineKind, + }, + ObjectMeta: metav1.ObjectMeta{ + Name: m.machine.Name, + Namespace: capiNamespace, + }, + Spec: spec, + }, warnings, errs +} + +// BareMetalProviderSpecFromRawExtension unmarshals a raw extension into a BareMetalMachineProviderSpec type. +func BareMetalProviderSpecFromRawExtension(rawExtension *runtime.RawExtension) (baremetalv1.BareMetalMachineProviderSpec, error) { + if rawExtension == nil { + return baremetalv1.BareMetalMachineProviderSpec{}, nil + } + + spec := baremetalv1.BareMetalMachineProviderSpec{} + if err := yaml.Unmarshal(rawExtension.Raw, &spec); err != nil { + return baremetalv1.BareMetalMachineProviderSpec{}, fmt.Errorf("error unmarshalling providerSpec: %w", err) + } + + return spec, nil +} + +func metal3MachineToMetal3MachineTemplate(metal3Machine *metal3v1.Metal3Machine, name string, namespace string) (*metal3v1.Metal3MachineTemplate, error) { + nameWithHash, err := util.GenerateInfraMachineTemplateNameWithSpecHash(name, metal3Machine.Spec) + if err != nil { + return nil, fmt.Errorf("failed to generate infrastructure machine template name with spec hash: %w", err) + } + + return &metal3v1.Metal3MachineTemplate{ + TypeMeta: metav1.TypeMeta{ + APIVersion: metal3v1.GroupVersion.String(), + Kind: metal3MachineTemplateKind, + }, + ObjectMeta: metav1.ObjectMeta{ + Name: nameWithHash, + Namespace: namespace, + }, + Spec: metal3v1.Metal3MachineTemplateSpec{ + Template: metal3v1.Metal3MachineTemplateResource{ + Spec: metal3Machine.Spec, + }, + }, + }, nil +} + +//////// Conversion helpers + +func convertBareMetalImageToMetal3(mapiImage baremetalv1.Image) metal3v1.Image { + return metal3v1.Image{ + URL: mapiImage.URL, + Checksum: mapiImage.Checksum, + // ChecksumType: Not present in MAPI, will use CAPM3 default. + // DiskFormat: Not present in MAPI, will use CAPM3 default. + } +} + +func convertBareMetalCustomDeployToMetal3(mapiCustomDeploy baremetalv1.CustomDeploy) *metal3v1.CustomDeploy { + if mapiCustomDeploy.Method == "" { + return nil + } + + return &metal3v1.CustomDeploy{ + Method: mapiCustomDeploy.Method, + } +} + +func convertBareMetalHostSelectorToMetal3(mapiHostSelector baremetalv1.HostSelector) metal3v1.HostSelector { + return metal3v1.HostSelector{ + MatchLabels: mapiHostSelector.MatchLabels, + MatchExpressions: convertBareMetalHostSelectorRequirementsToMetal3(mapiHostSelector.MatchExpressions), + } +} + +func convertBareMetalHostSelectorRequirementsToMetal3(mapiRequirements []baremetalv1.HostSelectorRequirement) []metal3v1.HostSelectorRequirement { + if mapiRequirements == nil { + return nil + } + + capiRequirements := make([]metal3v1.HostSelectorRequirement, 0, len(mapiRequirements)) + for _, req := range mapiRequirements { + capiRequirements = append(capiRequirements, metal3v1.HostSelectorRequirement{ + Key: req.Key, + Operator: req.Operator, + Values: req.Values, + }) + } + + return capiRequirements +} diff --git a/vendor/github.com/sergi/go-diff/AUTHORS b/vendor/github.com/sergi/go-diff/AUTHORS new file mode 100644 index 000000000..2d7bb2bf5 --- /dev/null +++ b/vendor/github.com/sergi/go-diff/AUTHORS @@ -0,0 +1,25 @@ +# This is the official list of go-diff authors for copyright purposes. +# This file is distinct from the CONTRIBUTORS files. +# See the latter for an explanation. + +# Names should be added to this file as +# Name or Organization +# The email address is not required for organizations. + +# Please keep the list sorted. + +Danny Yoo +James Kolb +Jonathan Amsterdam +Markus Zimmermann +Matt Kovars +Örjan Persson +Osman Masood +Robert Carlsen +Rory Flynn +Sergi Mansilla +Shatrugna Sadhu +Shawn Smith +Stas Maksimov +Tor Arvid Lund +Zac Bergquist diff --git a/vendor/github.com/sergi/go-diff/CONTRIBUTORS b/vendor/github.com/sergi/go-diff/CONTRIBUTORS new file mode 100644 index 000000000..369e3d551 --- /dev/null +++ b/vendor/github.com/sergi/go-diff/CONTRIBUTORS @@ -0,0 +1,32 @@ +# This is the official list of people who can contribute +# (and typically have contributed) code to the go-diff +# repository. +# +# The AUTHORS file lists the copyright holders; this file +# lists people. For example, ACME Inc. employees would be listed here +# but not in AUTHORS, because ACME Inc. would hold the copyright. +# +# When adding J Random Contributor's name to this file, +# either J's name or J's organization's name should be +# added to the AUTHORS file. +# +# Names should be added to this file like so: +# Name +# +# Please keep the list sorted. + +Danny Yoo +James Kolb +Jonathan Amsterdam +Markus Zimmermann +Matt Kovars +Örjan Persson +Osman Masood +Robert Carlsen +Rory Flynn +Sergi Mansilla +Shatrugna Sadhu +Shawn Smith +Stas Maksimov +Tor Arvid Lund +Zac Bergquist diff --git a/vendor/github.com/sergi/go-diff/LICENSE b/vendor/github.com/sergi/go-diff/LICENSE new file mode 100644 index 000000000..937942c2b --- /dev/null +++ b/vendor/github.com/sergi/go-diff/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2012-2016 The go-diff Authors. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + diff --git a/vendor/github.com/sergi/go-diff/diffmatchpatch/diff.go b/vendor/github.com/sergi/go-diff/diffmatchpatch/diff.go new file mode 100644 index 000000000..4f7b42488 --- /dev/null +++ b/vendor/github.com/sergi/go-diff/diffmatchpatch/diff.go @@ -0,0 +1,1352 @@ +// Copyright (c) 2012-2016 The go-diff authors. All rights reserved. +// https://github.com/sergi/go-diff +// See the included LICENSE file for license details. +// +// go-diff is a Go implementation of Google's Diff, Match, and Patch library +// Original library is Copyright (c) 2006 Google Inc. +// http://code.google.com/p/google-diff-match-patch/ + +package diffmatchpatch + +import ( + "bytes" + "errors" + "fmt" + "html" + "math" + "net/url" + "regexp" + "strconv" + "strings" + "time" + "unicode/utf8" +) + +// Operation defines the operation of a diff item. +type Operation int8 + +//go:generate stringer -type=Operation -trimprefix=Diff + +const ( + // DiffDelete item represents a delete diff. + DiffDelete Operation = -1 + // DiffInsert item represents an insert diff. + DiffInsert Operation = 1 + // DiffEqual item represents an equal diff. + DiffEqual Operation = 0 + //IndexSeparator is used to seperate the array indexes in an index string + IndexSeparator = "," +) + +// Diff represents one diff operation +type Diff struct { + Type Operation + Text string +} + +// splice removes amount elements from slice at index index, replacing them with elements. +func splice(slice []Diff, index int, amount int, elements ...Diff) []Diff { + if len(elements) == amount { + // Easy case: overwrite the relevant items. + copy(slice[index:], elements) + return slice + } + if len(elements) < amount { + // Fewer new items than old. + // Copy in the new items. + copy(slice[index:], elements) + // Shift the remaining items left. + copy(slice[index+len(elements):], slice[index+amount:]) + // Calculate the new end of the slice. + end := len(slice) - amount + len(elements) + // Zero stranded elements at end so that they can be garbage collected. + tail := slice[end:] + for i := range tail { + tail[i] = Diff{} + } + return slice[:end] + } + // More new items than old. + // Make room in slice for new elements. + // There's probably an even more efficient way to do this, + // but this is simple and clear. + need := len(slice) - amount + len(elements) + for len(slice) < need { + slice = append(slice, Diff{}) + } + // Shift slice elements right to make room for new elements. + copy(slice[index+len(elements):], slice[index+amount:]) + // Copy in new elements. + copy(slice[index:], elements) + return slice +} + +// DiffMain finds the differences between two texts. +// If an invalid UTF-8 sequence is encountered, it will be replaced by the Unicode replacement character. +func (dmp *DiffMatchPatch) DiffMain(text1, text2 string, checklines bool) []Diff { + return dmp.DiffMainRunes([]rune(text1), []rune(text2), checklines) +} + +// DiffMainRunes finds the differences between two rune sequences. +// If an invalid UTF-8 sequence is encountered, it will be replaced by the Unicode replacement character. +func (dmp *DiffMatchPatch) DiffMainRunes(text1, text2 []rune, checklines bool) []Diff { + var deadline time.Time + if dmp.DiffTimeout > 0 { + deadline = time.Now().Add(dmp.DiffTimeout) + } + return dmp.diffMainRunes(text1, text2, checklines, deadline) +} + +func (dmp *DiffMatchPatch) diffMainRunes(text1, text2 []rune, checklines bool, deadline time.Time) []Diff { + if runesEqual(text1, text2) { + var diffs []Diff + if len(text1) > 0 { + diffs = append(diffs, Diff{DiffEqual, string(text1)}) + } + return diffs + } + // Trim off common prefix (speedup). + commonlength := commonPrefixLength(text1, text2) + commonprefix := text1[:commonlength] + text1 = text1[commonlength:] + text2 = text2[commonlength:] + + // Trim off common suffix (speedup). + commonlength = commonSuffixLength(text1, text2) + commonsuffix := text1[len(text1)-commonlength:] + text1 = text1[:len(text1)-commonlength] + text2 = text2[:len(text2)-commonlength] + + // Compute the diff on the middle block. + diffs := dmp.diffCompute(text1, text2, checklines, deadline) + + // Restore the prefix and suffix. + if len(commonprefix) != 0 { + diffs = append([]Diff{{DiffEqual, string(commonprefix)}}, diffs...) + } + if len(commonsuffix) != 0 { + diffs = append(diffs, Diff{DiffEqual, string(commonsuffix)}) + } + + return dmp.DiffCleanupMerge(diffs) +} + +// diffCompute finds the differences between two rune slices. Assumes that the texts do not have any common prefix or suffix. +func (dmp *DiffMatchPatch) diffCompute(text1, text2 []rune, checklines bool, deadline time.Time) []Diff { + diffs := []Diff{} + if len(text1) == 0 { + // Just add some text (speedup). + return append(diffs, Diff{DiffInsert, string(text2)}) + } else if len(text2) == 0 { + // Just delete some text (speedup). + return append(diffs, Diff{DiffDelete, string(text1)}) + } + + var longtext, shorttext []rune + if len(text1) > len(text2) { + longtext = text1 + shorttext = text2 + } else { + longtext = text2 + shorttext = text1 + } + + if i := runesIndex(longtext, shorttext); i != -1 { + op := DiffInsert + // Swap insertions for deletions if diff is reversed. + if len(text1) > len(text2) { + op = DiffDelete + } + // Shorter text is inside the longer text (speedup). + return []Diff{ + Diff{op, string(longtext[:i])}, + Diff{DiffEqual, string(shorttext)}, + Diff{op, string(longtext[i+len(shorttext):])}, + } + } else if len(shorttext) == 1 { + // Single character string. + // After the previous speedup, the character can't be an equality. + return []Diff{ + {DiffDelete, string(text1)}, + {DiffInsert, string(text2)}, + } + // Check to see if the problem can be split in two. + } else if hm := dmp.diffHalfMatch(text1, text2); hm != nil { + // A half-match was found, sort out the return data. + text1A := hm[0] + text1B := hm[1] + text2A := hm[2] + text2B := hm[3] + midCommon := hm[4] + // Send both pairs off for separate processing. + diffsA := dmp.diffMainRunes(text1A, text2A, checklines, deadline) + diffsB := dmp.diffMainRunes(text1B, text2B, checklines, deadline) + // Merge the results. + diffs := diffsA + diffs = append(diffs, Diff{DiffEqual, string(midCommon)}) + diffs = append(diffs, diffsB...) + return diffs + } else if checklines && len(text1) > 100 && len(text2) > 100 { + return dmp.diffLineMode(text1, text2, deadline) + } + return dmp.diffBisect(text1, text2, deadline) +} + +// diffLineMode does a quick line-level diff on both []runes, then rediff the parts for greater accuracy. This speedup can produce non-minimal diffs. +func (dmp *DiffMatchPatch) diffLineMode(text1, text2 []rune, deadline time.Time) []Diff { + // Scan the text on a line-by-line basis first. + text1, text2, linearray := dmp.DiffLinesToRunes(string(text1), string(text2)) + + diffs := dmp.diffMainRunes(text1, text2, false, deadline) + + // Convert the diff back to original text. + diffs = dmp.DiffCharsToLines(diffs, linearray) + // Eliminate freak matches (e.g. blank lines) + diffs = dmp.DiffCleanupSemantic(diffs) + + // Rediff any replacement blocks, this time character-by-character. + // Add a dummy entry at the end. + diffs = append(diffs, Diff{DiffEqual, ""}) + + pointer := 0 + countDelete := 0 + countInsert := 0 + + // NOTE: Rune slices are slower than using strings in this case. + textDelete := "" + textInsert := "" + + for pointer < len(diffs) { + switch diffs[pointer].Type { + case DiffInsert: + countInsert++ + textInsert += diffs[pointer].Text + case DiffDelete: + countDelete++ + textDelete += diffs[pointer].Text + case DiffEqual: + // Upon reaching an equality, check for prior redundancies. + if countDelete >= 1 && countInsert >= 1 { + // Delete the offending records and add the merged ones. + diffs = splice(diffs, pointer-countDelete-countInsert, + countDelete+countInsert) + + pointer = pointer - countDelete - countInsert + a := dmp.diffMainRunes([]rune(textDelete), []rune(textInsert), false, deadline) + for j := len(a) - 1; j >= 0; j-- { + diffs = splice(diffs, pointer, 0, a[j]) + } + pointer = pointer + len(a) + } + + countInsert = 0 + countDelete = 0 + textDelete = "" + textInsert = "" + } + pointer++ + } + + return diffs[:len(diffs)-1] // Remove the dummy entry at the end. +} + +// DiffBisect finds the 'middle snake' of a diff, split the problem in two and return the recursively constructed diff. +// If an invalid UTF-8 sequence is encountered, it will be replaced by the Unicode replacement character. +// See Myers 1986 paper: An O(ND) Difference Algorithm and Its Variations. +func (dmp *DiffMatchPatch) DiffBisect(text1, text2 string, deadline time.Time) []Diff { + // Unused in this code, but retained for interface compatibility. + return dmp.diffBisect([]rune(text1), []rune(text2), deadline) +} + +// diffBisect finds the 'middle snake' of a diff, splits the problem in two and returns the recursively constructed diff. +// See Myers's 1986 paper: An O(ND) Difference Algorithm and Its Variations. +func (dmp *DiffMatchPatch) diffBisect(runes1, runes2 []rune, deadline time.Time) []Diff { + // Cache the text lengths to prevent multiple calls. + runes1Len, runes2Len := len(runes1), len(runes2) + + maxD := (runes1Len + runes2Len + 1) / 2 + vOffset := maxD + vLength := 2 * maxD + + v1 := make([]int, vLength) + v2 := make([]int, vLength) + for i := range v1 { + v1[i] = -1 + v2[i] = -1 + } + v1[vOffset+1] = 0 + v2[vOffset+1] = 0 + + delta := runes1Len - runes2Len + // If the total number of characters is odd, then the front path will collide with the reverse path. + front := (delta%2 != 0) + // Offsets for start and end of k loop. Prevents mapping of space beyond the grid. + k1start := 0 + k1end := 0 + k2start := 0 + k2end := 0 + for d := 0; d < maxD; d++ { + // Bail out if deadline is reached. + if !deadline.IsZero() && d%16 == 0 && time.Now().After(deadline) { + break + } + + // Walk the front path one step. + for k1 := -d + k1start; k1 <= d-k1end; k1 += 2 { + k1Offset := vOffset + k1 + var x1 int + + if k1 == -d || (k1 != d && v1[k1Offset-1] < v1[k1Offset+1]) { + x1 = v1[k1Offset+1] + } else { + x1 = v1[k1Offset-1] + 1 + } + + y1 := x1 - k1 + for x1 < runes1Len && y1 < runes2Len { + if runes1[x1] != runes2[y1] { + break + } + x1++ + y1++ + } + v1[k1Offset] = x1 + if x1 > runes1Len { + // Ran off the right of the graph. + k1end += 2 + } else if y1 > runes2Len { + // Ran off the bottom of the graph. + k1start += 2 + } else if front { + k2Offset := vOffset + delta - k1 + if k2Offset >= 0 && k2Offset < vLength && v2[k2Offset] != -1 { + // Mirror x2 onto top-left coordinate system. + x2 := runes1Len - v2[k2Offset] + if x1 >= x2 { + // Overlap detected. + return dmp.diffBisectSplit(runes1, runes2, x1, y1, deadline) + } + } + } + } + // Walk the reverse path one step. + for k2 := -d + k2start; k2 <= d-k2end; k2 += 2 { + k2Offset := vOffset + k2 + var x2 int + if k2 == -d || (k2 != d && v2[k2Offset-1] < v2[k2Offset+1]) { + x2 = v2[k2Offset+1] + } else { + x2 = v2[k2Offset-1] + 1 + } + var y2 = x2 - k2 + for x2 < runes1Len && y2 < runes2Len { + if runes1[runes1Len-x2-1] != runes2[runes2Len-y2-1] { + break + } + x2++ + y2++ + } + v2[k2Offset] = x2 + if x2 > runes1Len { + // Ran off the left of the graph. + k2end += 2 + } else if y2 > runes2Len { + // Ran off the top of the graph. + k2start += 2 + } else if !front { + k1Offset := vOffset + delta - k2 + if k1Offset >= 0 && k1Offset < vLength && v1[k1Offset] != -1 { + x1 := v1[k1Offset] + y1 := vOffset + x1 - k1Offset + // Mirror x2 onto top-left coordinate system. + x2 = runes1Len - x2 + if x1 >= x2 { + // Overlap detected. + return dmp.diffBisectSplit(runes1, runes2, x1, y1, deadline) + } + } + } + } + } + // Diff took too long and hit the deadline or number of diffs equals number of characters, no commonality at all. + return []Diff{ + {DiffDelete, string(runes1)}, + {DiffInsert, string(runes2)}, + } +} + +func (dmp *DiffMatchPatch) diffBisectSplit(runes1, runes2 []rune, x, y int, + deadline time.Time) []Diff { + runes1a := runes1[:x] + runes2a := runes2[:y] + runes1b := runes1[x:] + runes2b := runes2[y:] + + // Compute both diffs serially. + diffs := dmp.diffMainRunes(runes1a, runes2a, false, deadline) + diffsb := dmp.diffMainRunes(runes1b, runes2b, false, deadline) + + return append(diffs, diffsb...) +} + +// DiffLinesToChars splits two texts into a list of strings, and educes the texts to a string of hashes where each Unicode character represents one line. +// It's slightly faster to call DiffLinesToRunes first, followed by DiffMainRunes. +func (dmp *DiffMatchPatch) DiffLinesToChars(text1, text2 string) (string, string, []string) { + chars1, chars2, lineArray := dmp.diffLinesToStrings(text1, text2) + return chars1, chars2, lineArray +} + +// DiffLinesToRunes splits two texts into a list of runes. +func (dmp *DiffMatchPatch) DiffLinesToRunes(text1, text2 string) ([]rune, []rune, []string) { + chars1, chars2, lineArray := dmp.diffLinesToStrings(text1, text2) + return []rune(chars1), []rune(chars2), lineArray +} + +// DiffCharsToLines rehydrates the text in a diff from a string of line hashes to real lines of text. +func (dmp *DiffMatchPatch) DiffCharsToLines(diffs []Diff, lineArray []string) []Diff { + hydrated := make([]Diff, 0, len(diffs)) + for _, aDiff := range diffs { + chars := strings.Split(aDiff.Text, IndexSeparator) + text := make([]string, len(chars)) + + for i, r := range chars { + i1, err := strconv.Atoi(r) + if err == nil { + text[i] = lineArray[i1] + } + } + + aDiff.Text = strings.Join(text, "") + hydrated = append(hydrated, aDiff) + } + return hydrated +} + +// DiffCommonPrefix determines the common prefix length of two strings. +func (dmp *DiffMatchPatch) DiffCommonPrefix(text1, text2 string) int { + // Unused in this code, but retained for interface compatibility. + return commonPrefixLength([]rune(text1), []rune(text2)) +} + +// DiffCommonSuffix determines the common suffix length of two strings. +func (dmp *DiffMatchPatch) DiffCommonSuffix(text1, text2 string) int { + // Unused in this code, but retained for interface compatibility. + return commonSuffixLength([]rune(text1), []rune(text2)) +} + +// commonPrefixLength returns the length of the common prefix of two rune slices. +func commonPrefixLength(text1, text2 []rune) int { + // Linear search. See comment in commonSuffixLength. + n := 0 + for ; n < len(text1) && n < len(text2); n++ { + if text1[n] != text2[n] { + return n + } + } + return n +} + +// commonSuffixLength returns the length of the common suffix of two rune slices. +func commonSuffixLength(text1, text2 []rune) int { + // Use linear search rather than the binary search discussed at https://neil.fraser.name/news/2007/10/09/. + // See discussion at https://github.com/sergi/go-diff/issues/54. + i1 := len(text1) + i2 := len(text2) + for n := 0; ; n++ { + i1-- + i2-- + if i1 < 0 || i2 < 0 || text1[i1] != text2[i2] { + return n + } + } +} + +// DiffCommonOverlap determines if the suffix of one string is the prefix of another. +func (dmp *DiffMatchPatch) DiffCommonOverlap(text1 string, text2 string) int { + // Cache the text lengths to prevent multiple calls. + text1Length := len(text1) + text2Length := len(text2) + // Eliminate the null case. + if text1Length == 0 || text2Length == 0 { + return 0 + } + // Truncate the longer string. + if text1Length > text2Length { + text1 = text1[text1Length-text2Length:] + } else if text1Length < text2Length { + text2 = text2[0:text1Length] + } + textLength := int(math.Min(float64(text1Length), float64(text2Length))) + // Quick check for the worst case. + if text1 == text2 { + return textLength + } + + // Start by looking for a single character match and increase length until no match is found. Performance analysis: http://neil.fraser.name/news/2010/11/04/ + best := 0 + length := 1 + for { + pattern := text1[textLength-length:] + found := strings.Index(text2, pattern) + if found == -1 { + break + } + length += found + if found == 0 || text1[textLength-length:] == text2[0:length] { + best = length + length++ + } + } + + return best +} + +// DiffHalfMatch checks whether the two texts share a substring which is at least half the length of the longer text. This speedup can produce non-minimal diffs. +func (dmp *DiffMatchPatch) DiffHalfMatch(text1, text2 string) []string { + // Unused in this code, but retained for interface compatibility. + runeSlices := dmp.diffHalfMatch([]rune(text1), []rune(text2)) + if runeSlices == nil { + return nil + } + + result := make([]string, len(runeSlices)) + for i, r := range runeSlices { + result[i] = string(r) + } + return result +} + +func (dmp *DiffMatchPatch) diffHalfMatch(text1, text2 []rune) [][]rune { + if dmp.DiffTimeout <= 0 { + // Don't risk returning a non-optimal diff if we have unlimited time. + return nil + } + + var longtext, shorttext []rune + if len(text1) > len(text2) { + longtext = text1 + shorttext = text2 + } else { + longtext = text2 + shorttext = text1 + } + + if len(longtext) < 4 || len(shorttext)*2 < len(longtext) { + return nil // Pointless. + } + + // First check if the second quarter is the seed for a half-match. + hm1 := dmp.diffHalfMatchI(longtext, shorttext, int(float64(len(longtext)+3)/4)) + + // Check again based on the third quarter. + hm2 := dmp.diffHalfMatchI(longtext, shorttext, int(float64(len(longtext)+1)/2)) + + hm := [][]rune{} + if hm1 == nil && hm2 == nil { + return nil + } else if hm2 == nil { + hm = hm1 + } else if hm1 == nil { + hm = hm2 + } else { + // Both matched. Select the longest. + if len(hm1[4]) > len(hm2[4]) { + hm = hm1 + } else { + hm = hm2 + } + } + + // A half-match was found, sort out the return data. + if len(text1) > len(text2) { + return hm + } + + return [][]rune{hm[2], hm[3], hm[0], hm[1], hm[4]} +} + +// diffHalfMatchI checks if a substring of shorttext exist within longtext such that the substring is at least half the length of longtext? +// Returns a slice containing the prefix of longtext, the suffix of longtext, the prefix of shorttext, the suffix of shorttext and the common middle, or null if there was no match. +func (dmp *DiffMatchPatch) diffHalfMatchI(l, s []rune, i int) [][]rune { + var bestCommonA []rune + var bestCommonB []rune + var bestCommonLen int + var bestLongtextA []rune + var bestLongtextB []rune + var bestShorttextA []rune + var bestShorttextB []rune + + // Start with a 1/4 length substring at position i as a seed. + seed := l[i : i+len(l)/4] + + for j := runesIndexOf(s, seed, 0); j != -1; j = runesIndexOf(s, seed, j+1) { + prefixLength := commonPrefixLength(l[i:], s[j:]) + suffixLength := commonSuffixLength(l[:i], s[:j]) + + if bestCommonLen < suffixLength+prefixLength { + bestCommonA = s[j-suffixLength : j] + bestCommonB = s[j : j+prefixLength] + bestCommonLen = len(bestCommonA) + len(bestCommonB) + bestLongtextA = l[:i-suffixLength] + bestLongtextB = l[i+prefixLength:] + bestShorttextA = s[:j-suffixLength] + bestShorttextB = s[j+prefixLength:] + } + } + + if bestCommonLen*2 < len(l) { + return nil + } + + return [][]rune{ + bestLongtextA, + bestLongtextB, + bestShorttextA, + bestShorttextB, + append(bestCommonA, bestCommonB...), + } +} + +// DiffCleanupSemantic reduces the number of edits by eliminating semantically trivial equalities. +func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff { + changes := false + // Stack of indices where equalities are found. + equalities := make([]int, 0, len(diffs)) + + var lastequality string + // Always equal to diffs[equalities[equalitiesLength - 1]][1] + var pointer int // Index of current position. + // Number of characters that changed prior to the equality. + var lengthInsertions1, lengthDeletions1 int + // Number of characters that changed after the equality. + var lengthInsertions2, lengthDeletions2 int + + for pointer < len(diffs) { + if diffs[pointer].Type == DiffEqual { + // Equality found. + equalities = append(equalities, pointer) + lengthInsertions1 = lengthInsertions2 + lengthDeletions1 = lengthDeletions2 + lengthInsertions2 = 0 + lengthDeletions2 = 0 + lastequality = diffs[pointer].Text + } else { + // An insertion or deletion. + + if diffs[pointer].Type == DiffInsert { + lengthInsertions2 += utf8.RuneCountInString(diffs[pointer].Text) + } else { + lengthDeletions2 += utf8.RuneCountInString(diffs[pointer].Text) + } + // Eliminate an equality that is smaller or equal to the edits on both sides of it. + difference1 := int(math.Max(float64(lengthInsertions1), float64(lengthDeletions1))) + difference2 := int(math.Max(float64(lengthInsertions2), float64(lengthDeletions2))) + if utf8.RuneCountInString(lastequality) > 0 && + (utf8.RuneCountInString(lastequality) <= difference1) && + (utf8.RuneCountInString(lastequality) <= difference2) { + // Duplicate record. + insPoint := equalities[len(equalities)-1] + diffs = splice(diffs, insPoint, 0, Diff{DiffDelete, lastequality}) + + // Change second copy to insert. + diffs[insPoint+1].Type = DiffInsert + // Throw away the equality we just deleted. + equalities = equalities[:len(equalities)-1] + + if len(equalities) > 0 { + equalities = equalities[:len(equalities)-1] + } + pointer = -1 + if len(equalities) > 0 { + pointer = equalities[len(equalities)-1] + } + + lengthInsertions1 = 0 // Reset the counters. + lengthDeletions1 = 0 + lengthInsertions2 = 0 + lengthDeletions2 = 0 + lastequality = "" + changes = true + } + } + pointer++ + } + + // Normalize the diff. + if changes { + diffs = dmp.DiffCleanupMerge(diffs) + } + diffs = dmp.DiffCleanupSemanticLossless(diffs) + // Find any overlaps between deletions and insertions. + // e.g: abcxxxxxxdef + // -> abcxxxdef + // e.g: xxxabcdefxxx + // -> defxxxabc + // Only extract an overlap if it is as big as the edit ahead or behind it. + pointer = 1 + for pointer < len(diffs) { + if diffs[pointer-1].Type == DiffDelete && + diffs[pointer].Type == DiffInsert { + deletion := diffs[pointer-1].Text + insertion := diffs[pointer].Text + overlapLength1 := dmp.DiffCommonOverlap(deletion, insertion) + overlapLength2 := dmp.DiffCommonOverlap(insertion, deletion) + if overlapLength1 >= overlapLength2 { + if float64(overlapLength1) >= float64(utf8.RuneCountInString(deletion))/2 || + float64(overlapLength1) >= float64(utf8.RuneCountInString(insertion))/2 { + + // Overlap found. Insert an equality and trim the surrounding edits. + diffs = splice(diffs, pointer, 0, Diff{DiffEqual, insertion[:overlapLength1]}) + diffs[pointer-1].Text = + deletion[0 : len(deletion)-overlapLength1] + diffs[pointer+1].Text = insertion[overlapLength1:] + pointer++ + } + } else { + if float64(overlapLength2) >= float64(utf8.RuneCountInString(deletion))/2 || + float64(overlapLength2) >= float64(utf8.RuneCountInString(insertion))/2 { + // Reverse overlap found. Insert an equality and swap and trim the surrounding edits. + overlap := Diff{DiffEqual, deletion[:overlapLength2]} + diffs = splice(diffs, pointer, 0, overlap) + diffs[pointer-1].Type = DiffInsert + diffs[pointer-1].Text = insertion[0 : len(insertion)-overlapLength2] + diffs[pointer+1].Type = DiffDelete + diffs[pointer+1].Text = deletion[overlapLength2:] + pointer++ + } + } + pointer++ + } + pointer++ + } + + return diffs +} + +// Define some regex patterns for matching boundaries. +var ( + nonAlphaNumericRegex = regexp.MustCompile(`[^a-zA-Z0-9]`) + whitespaceRegex = regexp.MustCompile(`\s`) + linebreakRegex = regexp.MustCompile(`[\r\n]`) + blanklineEndRegex = regexp.MustCompile(`\n\r?\n$`) + blanklineStartRegex = regexp.MustCompile(`^\r?\n\r?\n`) +) + +// diffCleanupSemanticScore computes a score representing whether the internal boundary falls on logical boundaries. +// Scores range from 6 (best) to 0 (worst). Closure, but does not reference any external variables. +func diffCleanupSemanticScore(one, two string) int { + if len(one) == 0 || len(two) == 0 { + // Edges are the best. + return 6 + } + + // Each port of this function behaves slightly differently due to subtle differences in each language's definition of things like 'whitespace'. Since this function's purpose is largely cosmetic, the choice has been made to use each language's native features rather than force total conformity. + rune1, _ := utf8.DecodeLastRuneInString(one) + rune2, _ := utf8.DecodeRuneInString(two) + char1 := string(rune1) + char2 := string(rune2) + + nonAlphaNumeric1 := nonAlphaNumericRegex.MatchString(char1) + nonAlphaNumeric2 := nonAlphaNumericRegex.MatchString(char2) + whitespace1 := nonAlphaNumeric1 && whitespaceRegex.MatchString(char1) + whitespace2 := nonAlphaNumeric2 && whitespaceRegex.MatchString(char2) + lineBreak1 := whitespace1 && linebreakRegex.MatchString(char1) + lineBreak2 := whitespace2 && linebreakRegex.MatchString(char2) + blankLine1 := lineBreak1 && blanklineEndRegex.MatchString(one) + blankLine2 := lineBreak2 && blanklineEndRegex.MatchString(two) + + if blankLine1 || blankLine2 { + // Five points for blank lines. + return 5 + } else if lineBreak1 || lineBreak2 { + // Four points for line breaks. + return 4 + } else if nonAlphaNumeric1 && !whitespace1 && whitespace2 { + // Three points for end of sentences. + return 3 + } else if whitespace1 || whitespace2 { + // Two points for whitespace. + return 2 + } else if nonAlphaNumeric1 || nonAlphaNumeric2 { + // One point for non-alphanumeric. + return 1 + } + return 0 +} + +// DiffCleanupSemanticLossless looks for single edits surrounded on both sides by equalities which can be shifted sideways to align the edit to a word boundary. +// E.g: The cat came. -> The cat came. +func (dmp *DiffMatchPatch) DiffCleanupSemanticLossless(diffs []Diff) []Diff { + pointer := 1 + + // Intentionally ignore the first and last element (don't need checking). + for pointer < len(diffs)-1 { + if diffs[pointer-1].Type == DiffEqual && + diffs[pointer+1].Type == DiffEqual { + + // This is a single edit surrounded by equalities. + equality1 := diffs[pointer-1].Text + edit := diffs[pointer].Text + equality2 := diffs[pointer+1].Text + + // First, shift the edit as far left as possible. + commonOffset := dmp.DiffCommonSuffix(equality1, edit) + if commonOffset > 0 { + commonString := edit[len(edit)-commonOffset:] + equality1 = equality1[0 : len(equality1)-commonOffset] + edit = commonString + edit[:len(edit)-commonOffset] + equality2 = commonString + equality2 + } + + // Second, step character by character right, looking for the best fit. + bestEquality1 := equality1 + bestEdit := edit + bestEquality2 := equality2 + bestScore := diffCleanupSemanticScore(equality1, edit) + + diffCleanupSemanticScore(edit, equality2) + + for len(edit) != 0 && len(equality2) != 0 { + _, sz := utf8.DecodeRuneInString(edit) + if len(equality2) < sz || edit[:sz] != equality2[:sz] { + break + } + equality1 += edit[:sz] + edit = edit[sz:] + equality2[:sz] + equality2 = equality2[sz:] + score := diffCleanupSemanticScore(equality1, edit) + + diffCleanupSemanticScore(edit, equality2) + // The >= encourages trailing rather than leading whitespace on edits. + if score >= bestScore { + bestScore = score + bestEquality1 = equality1 + bestEdit = edit + bestEquality2 = equality2 + } + } + + if diffs[pointer-1].Text != bestEquality1 { + // We have an improvement, save it back to the diff. + if len(bestEquality1) != 0 { + diffs[pointer-1].Text = bestEquality1 + } else { + diffs = splice(diffs, pointer-1, 1) + pointer-- + } + + diffs[pointer].Text = bestEdit + if len(bestEquality2) != 0 { + diffs[pointer+1].Text = bestEquality2 + } else { + diffs = append(diffs[:pointer+1], diffs[pointer+2:]...) + pointer-- + } + } + } + pointer++ + } + + return diffs +} + +// DiffCleanupEfficiency reduces the number of edits by eliminating operationally trivial equalities. +func (dmp *DiffMatchPatch) DiffCleanupEfficiency(diffs []Diff) []Diff { + changes := false + // Stack of indices where equalities are found. + type equality struct { + data int + next *equality + } + var equalities *equality + // Always equal to equalities[equalitiesLength-1][1] + lastequality := "" + pointer := 0 // Index of current position. + // Is there an insertion operation before the last equality. + preIns := false + // Is there a deletion operation before the last equality. + preDel := false + // Is there an insertion operation after the last equality. + postIns := false + // Is there a deletion operation after the last equality. + postDel := false + for pointer < len(diffs) { + if diffs[pointer].Type == DiffEqual { // Equality found. + if len(diffs[pointer].Text) < dmp.DiffEditCost && + (postIns || postDel) { + // Candidate found. + equalities = &equality{ + data: pointer, + next: equalities, + } + preIns = postIns + preDel = postDel + lastequality = diffs[pointer].Text + } else { + // Not a candidate, and can never become one. + equalities = nil + lastequality = "" + } + postIns = false + postDel = false + } else { // An insertion or deletion. + if diffs[pointer].Type == DiffDelete { + postDel = true + } else { + postIns = true + } + + // Five types to be split: + // ABXYCD + // AXCD + // ABXC + // AXCD + // ABXC + var sumPres int + if preIns { + sumPres++ + } + if preDel { + sumPres++ + } + if postIns { + sumPres++ + } + if postDel { + sumPres++ + } + if len(lastequality) > 0 && + ((preIns && preDel && postIns && postDel) || + ((len(lastequality) < dmp.DiffEditCost/2) && sumPres == 3)) { + + insPoint := equalities.data + + // Duplicate record. + diffs = splice(diffs, insPoint, 0, Diff{DiffDelete, lastequality}) + + // Change second copy to insert. + diffs[insPoint+1].Type = DiffInsert + // Throw away the equality we just deleted. + equalities = equalities.next + lastequality = "" + + if preIns && preDel { + // No changes made which could affect previous entry, keep going. + postIns = true + postDel = true + equalities = nil + } else { + if equalities != nil { + equalities = equalities.next + } + if equalities != nil { + pointer = equalities.data + } else { + pointer = -1 + } + postIns = false + postDel = false + } + changes = true + } + } + pointer++ + } + + if changes { + diffs = dmp.DiffCleanupMerge(diffs) + } + + return diffs +} + +// DiffCleanupMerge reorders and merges like edit sections. Merge equalities. +// Any edit section can move as long as it doesn't cross an equality. +func (dmp *DiffMatchPatch) DiffCleanupMerge(diffs []Diff) []Diff { + // Add a dummy entry at the end. + diffs = append(diffs, Diff{DiffEqual, ""}) + pointer := 0 + countDelete := 0 + countInsert := 0 + commonlength := 0 + textDelete := []rune(nil) + textInsert := []rune(nil) + + for pointer < len(diffs) { + switch diffs[pointer].Type { + case DiffInsert: + countInsert++ + textInsert = append(textInsert, []rune(diffs[pointer].Text)...) + pointer++ + break + case DiffDelete: + countDelete++ + textDelete = append(textDelete, []rune(diffs[pointer].Text)...) + pointer++ + break + case DiffEqual: + // Upon reaching an equality, check for prior redundancies. + if countDelete+countInsert > 1 { + if countDelete != 0 && countInsert != 0 { + // Factor out any common prefixies. + commonlength = commonPrefixLength(textInsert, textDelete) + if commonlength != 0 { + x := pointer - countDelete - countInsert + if x > 0 && diffs[x-1].Type == DiffEqual { + diffs[x-1].Text += string(textInsert[:commonlength]) + } else { + diffs = append([]Diff{{DiffEqual, string(textInsert[:commonlength])}}, diffs...) + pointer++ + } + textInsert = textInsert[commonlength:] + textDelete = textDelete[commonlength:] + } + // Factor out any common suffixies. + commonlength = commonSuffixLength(textInsert, textDelete) + if commonlength != 0 { + insertIndex := len(textInsert) - commonlength + deleteIndex := len(textDelete) - commonlength + diffs[pointer].Text = string(textInsert[insertIndex:]) + diffs[pointer].Text + textInsert = textInsert[:insertIndex] + textDelete = textDelete[:deleteIndex] + } + } + // Delete the offending records and add the merged ones. + if countDelete == 0 { + diffs = splice(diffs, pointer-countInsert, + countDelete+countInsert, + Diff{DiffInsert, string(textInsert)}) + } else if countInsert == 0 { + diffs = splice(diffs, pointer-countDelete, + countDelete+countInsert, + Diff{DiffDelete, string(textDelete)}) + } else { + diffs = splice(diffs, pointer-countDelete-countInsert, + countDelete+countInsert, + Diff{DiffDelete, string(textDelete)}, + Diff{DiffInsert, string(textInsert)}) + } + + pointer = pointer - countDelete - countInsert + 1 + if countDelete != 0 { + pointer++ + } + if countInsert != 0 { + pointer++ + } + } else if pointer != 0 && diffs[pointer-1].Type == DiffEqual { + // Merge this equality with the previous one. + diffs[pointer-1].Text += diffs[pointer].Text + diffs = append(diffs[:pointer], diffs[pointer+1:]...) + } else { + pointer++ + } + countInsert = 0 + countDelete = 0 + textDelete = nil + textInsert = nil + break + } + } + + if len(diffs[len(diffs)-1].Text) == 0 { + diffs = diffs[0 : len(diffs)-1] // Remove the dummy entry at the end. + } + + // Second pass: look for single edits surrounded on both sides by equalities which can be shifted sideways to eliminate an equality. E.g: ABAC -> ABAC + changes := false + pointer = 1 + // Intentionally ignore the first and last element (don't need checking). + for pointer < (len(diffs) - 1) { + if diffs[pointer-1].Type == DiffEqual && + diffs[pointer+1].Type == DiffEqual { + // This is a single edit surrounded by equalities. + if strings.HasSuffix(diffs[pointer].Text, diffs[pointer-1].Text) { + // Shift the edit over the previous equality. + diffs[pointer].Text = diffs[pointer-1].Text + + diffs[pointer].Text[:len(diffs[pointer].Text)-len(diffs[pointer-1].Text)] + diffs[pointer+1].Text = diffs[pointer-1].Text + diffs[pointer+1].Text + diffs = splice(diffs, pointer-1, 1) + changes = true + } else if strings.HasPrefix(diffs[pointer].Text, diffs[pointer+1].Text) { + // Shift the edit over the next equality. + diffs[pointer-1].Text += diffs[pointer+1].Text + diffs[pointer].Text = + diffs[pointer].Text[len(diffs[pointer+1].Text):] + diffs[pointer+1].Text + diffs = splice(diffs, pointer+1, 1) + changes = true + } + } + pointer++ + } + + // If shifts were made, the diff needs reordering and another shift sweep. + if changes { + diffs = dmp.DiffCleanupMerge(diffs) + } + + return diffs +} + +// DiffXIndex returns the equivalent location in s2. +func (dmp *DiffMatchPatch) DiffXIndex(diffs []Diff, loc int) int { + chars1 := 0 + chars2 := 0 + lastChars1 := 0 + lastChars2 := 0 + lastDiff := Diff{} + for i := 0; i < len(diffs); i++ { + aDiff := diffs[i] + if aDiff.Type != DiffInsert { + // Equality or deletion. + chars1 += len(aDiff.Text) + } + if aDiff.Type != DiffDelete { + // Equality or insertion. + chars2 += len(aDiff.Text) + } + if chars1 > loc { + // Overshot the location. + lastDiff = aDiff + break + } + lastChars1 = chars1 + lastChars2 = chars2 + } + if lastDiff.Type == DiffDelete { + // The location was deleted. + return lastChars2 + } + // Add the remaining character length. + return lastChars2 + (loc - lastChars1) +} + +// DiffPrettyHtml converts a []Diff into a pretty HTML report. +// It is intended as an example from which to write one's own display functions. +func (dmp *DiffMatchPatch) DiffPrettyHtml(diffs []Diff) string { + var buff bytes.Buffer + for _, diff := range diffs { + text := strings.Replace(html.EscapeString(diff.Text), "\n", "¶
", -1) + switch diff.Type { + case DiffInsert: + _, _ = buff.WriteString("") + _, _ = buff.WriteString(text) + _, _ = buff.WriteString("") + case DiffDelete: + _, _ = buff.WriteString("") + _, _ = buff.WriteString(text) + _, _ = buff.WriteString("") + case DiffEqual: + _, _ = buff.WriteString("") + _, _ = buff.WriteString(text) + _, _ = buff.WriteString("") + } + } + return buff.String() +} + +// DiffPrettyText converts a []Diff into a colored text report. +func (dmp *DiffMatchPatch) DiffPrettyText(diffs []Diff) string { + var buff bytes.Buffer + for _, diff := range diffs { + text := diff.Text + + switch diff.Type { + case DiffInsert: + _, _ = buff.WriteString("\x1b[32m") + _, _ = buff.WriteString(text) + _, _ = buff.WriteString("\x1b[0m") + case DiffDelete: + _, _ = buff.WriteString("\x1b[31m") + _, _ = buff.WriteString(text) + _, _ = buff.WriteString("\x1b[0m") + case DiffEqual: + _, _ = buff.WriteString(text) + } + } + + return buff.String() +} + +// DiffText1 computes and returns the source text (all equalities and deletions). +func (dmp *DiffMatchPatch) DiffText1(diffs []Diff) string { + //StringBuilder text = new StringBuilder() + var text bytes.Buffer + + for _, aDiff := range diffs { + if aDiff.Type != DiffInsert { + _, _ = text.WriteString(aDiff.Text) + } + } + return text.String() +} + +// DiffText2 computes and returns the destination text (all equalities and insertions). +func (dmp *DiffMatchPatch) DiffText2(diffs []Diff) string { + var text bytes.Buffer + + for _, aDiff := range diffs { + if aDiff.Type != DiffDelete { + _, _ = text.WriteString(aDiff.Text) + } + } + return text.String() +} + +// DiffLevenshtein computes the Levenshtein distance that is the number of inserted, deleted or substituted characters. +func (dmp *DiffMatchPatch) DiffLevenshtein(diffs []Diff) int { + levenshtein := 0 + insertions := 0 + deletions := 0 + + for _, aDiff := range diffs { + switch aDiff.Type { + case DiffInsert: + insertions += utf8.RuneCountInString(aDiff.Text) + case DiffDelete: + deletions += utf8.RuneCountInString(aDiff.Text) + case DiffEqual: + // A deletion and an insertion is one substitution. + levenshtein += max(insertions, deletions) + insertions = 0 + deletions = 0 + } + } + + levenshtein += max(insertions, deletions) + return levenshtein +} + +// DiffToDelta crushes the diff into an encoded string which describes the operations required to transform text1 into text2. +// E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'. Operations are tab-separated. Inserted text is escaped using %xx notation. +func (dmp *DiffMatchPatch) DiffToDelta(diffs []Diff) string { + var text bytes.Buffer + for _, aDiff := range diffs { + switch aDiff.Type { + case DiffInsert: + _, _ = text.WriteString("+") + _, _ = text.WriteString(strings.Replace(url.QueryEscape(aDiff.Text), "+", " ", -1)) + _, _ = text.WriteString("\t") + break + case DiffDelete: + _, _ = text.WriteString("-") + _, _ = text.WriteString(strconv.Itoa(utf8.RuneCountInString(aDiff.Text))) + _, _ = text.WriteString("\t") + break + case DiffEqual: + _, _ = text.WriteString("=") + _, _ = text.WriteString(strconv.Itoa(utf8.RuneCountInString(aDiff.Text))) + _, _ = text.WriteString("\t") + break + } + } + delta := text.String() + if len(delta) != 0 { + // Strip off trailing tab character. + delta = delta[0 : utf8.RuneCountInString(delta)-1] + delta = unescaper.Replace(delta) + } + return delta +} + +// DiffFromDelta given the original text1, and an encoded string which describes the operations required to transform text1 into text2, comAdde the full diff. +func (dmp *DiffMatchPatch) DiffFromDelta(text1 string, delta string) (diffs []Diff, err error) { + i := 0 + runes := []rune(text1) + + for _, token := range strings.Split(delta, "\t") { + if len(token) == 0 { + // Blank tokens are ok (from a trailing \t). + continue + } + + // Each token begins with a one character parameter which specifies the operation of this token (delete, insert, equality). + param := token[1:] + + switch op := token[0]; op { + case '+': + // Decode would Diff all "+" to " " + param = strings.Replace(param, "+", "%2b", -1) + param, err = url.QueryUnescape(param) + if err != nil { + return nil, err + } + if !utf8.ValidString(param) { + return nil, fmt.Errorf("invalid UTF-8 token: %q", param) + } + + diffs = append(diffs, Diff{DiffInsert, param}) + case '=', '-': + n, err := strconv.ParseInt(param, 10, 0) + if err != nil { + return nil, err + } else if n < 0 { + return nil, errors.New("Negative number in DiffFromDelta: " + param) + } + + i += int(n) + // Break out if we are out of bounds, go1.6 can't handle this very well + if i > len(runes) { + break + } + // Remember that string slicing is by byte - we want by rune here. + text := string(runes[i-int(n) : i]) + + if op == '=' { + diffs = append(diffs, Diff{DiffEqual, text}) + } else { + diffs = append(diffs, Diff{DiffDelete, text}) + } + default: + // Anything else is an error. + return nil, errors.New("Invalid diff operation in DiffFromDelta: " + string(token[0])) + } + } + + if i != len(runes) { + return nil, fmt.Errorf("Delta length (%v) is different from source text length (%v)", i, len(text1)) + } + + return diffs, nil +} + +// diffLinesToStrings splits two texts into a list of strings. Each string represents one line. +func (dmp *DiffMatchPatch) diffLinesToStrings(text1, text2 string) (string, string, []string) { + // '\x00' is a valid character, but various debuggers don't like it. So we'll insert a junk entry to avoid generating a null character. + lineArray := []string{""} // e.g. lineArray[4] == 'Hello\n' + + lineHash := make(map[string]int) + //Each string has the index of lineArray which it points to + strIndexArray1 := dmp.diffLinesToStringsMunge(text1, &lineArray, lineHash) + strIndexArray2 := dmp.diffLinesToStringsMunge(text2, &lineArray, lineHash) + + return intArrayToString(strIndexArray1), intArrayToString(strIndexArray2), lineArray +} + +// diffLinesToStringsMunge splits a text into an array of strings, and reduces the texts to a []string. +func (dmp *DiffMatchPatch) diffLinesToStringsMunge(text string, lineArray *[]string, lineHash map[string]int) []uint32 { + // Walk the text, pulling out a substring for each line. text.split('\n') would would temporarily double our memory footprint. Modifying text would create many large strings to garbage collect. + lineStart := 0 + lineEnd := -1 + strs := []uint32{} + + for lineEnd < len(text)-1 { + lineEnd = indexOf(text, "\n", lineStart) + + if lineEnd == -1 { + lineEnd = len(text) - 1 + } + + line := text[lineStart : lineEnd+1] + lineStart = lineEnd + 1 + lineValue, ok := lineHash[line] + + if ok { + strs = append(strs, uint32(lineValue)) + } else { + *lineArray = append(*lineArray, line) + lineHash[line] = len(*lineArray) - 1 + strs = append(strs, uint32(len(*lineArray)-1)) + } + } + + return strs +} diff --git a/vendor/github.com/sergi/go-diff/diffmatchpatch/diffmatchpatch.go b/vendor/github.com/sergi/go-diff/diffmatchpatch/diffmatchpatch.go new file mode 100644 index 000000000..d3acc32ce --- /dev/null +++ b/vendor/github.com/sergi/go-diff/diffmatchpatch/diffmatchpatch.go @@ -0,0 +1,46 @@ +// Copyright (c) 2012-2016 The go-diff authors. All rights reserved. +// https://github.com/sergi/go-diff +// See the included LICENSE file for license details. +// +// go-diff is a Go implementation of Google's Diff, Match, and Patch library +// Original library is Copyright (c) 2006 Google Inc. +// http://code.google.com/p/google-diff-match-patch/ + +// Package diffmatchpatch offers robust algorithms to perform the operations required for synchronizing plain text. +package diffmatchpatch + +import ( + "time" +) + +// DiffMatchPatch holds the configuration for diff-match-patch operations. +type DiffMatchPatch struct { + // Number of seconds to map a diff before giving up (0 for infinity). + DiffTimeout time.Duration + // Cost of an empty edit operation in terms of edit characters. + DiffEditCost int + // How far to search for a match (0 = exact location, 1000+ = broad match). A match this many characters away from the expected location will add 1.0 to the score (0.0 is a perfect match). + MatchDistance int + // When deleting a large block of text (over ~64 characters), how close do the contents have to be to match the expected contents. (0.0 = perfection, 1.0 = very loose). Note that MatchThreshold controls how closely the end points of a delete need to match. + PatchDeleteThreshold float64 + // Chunk size for context length. + PatchMargin int + // The number of bits in an int. + MatchMaxBits int + // At what point is no match declared (0.0 = perfection, 1.0 = very loose). + MatchThreshold float64 +} + +// New creates a new DiffMatchPatch object with default parameters. +func New() *DiffMatchPatch { + // Defaults. + return &DiffMatchPatch{ + DiffTimeout: time.Second, + DiffEditCost: 4, + MatchThreshold: 0.5, + MatchDistance: 1000, + PatchDeleteThreshold: 0.5, + PatchMargin: 4, + MatchMaxBits: 32, + } +} diff --git a/vendor/github.com/sergi/go-diff/diffmatchpatch/match.go b/vendor/github.com/sergi/go-diff/diffmatchpatch/match.go new file mode 100644 index 000000000..17374e109 --- /dev/null +++ b/vendor/github.com/sergi/go-diff/diffmatchpatch/match.go @@ -0,0 +1,160 @@ +// Copyright (c) 2012-2016 The go-diff authors. All rights reserved. +// https://github.com/sergi/go-diff +// See the included LICENSE file for license details. +// +// go-diff is a Go implementation of Google's Diff, Match, and Patch library +// Original library is Copyright (c) 2006 Google Inc. +// http://code.google.com/p/google-diff-match-patch/ + +package diffmatchpatch + +import ( + "math" +) + +// MatchMain locates the best instance of 'pattern' in 'text' near 'loc'. +// Returns -1 if no match found. +func (dmp *DiffMatchPatch) MatchMain(text, pattern string, loc int) int { + // Check for null inputs not needed since null can't be passed in C#. + + loc = int(math.Max(0, math.Min(float64(loc), float64(len(text))))) + if text == pattern { + // Shortcut (potentially not guaranteed by the algorithm) + return 0 + } else if len(text) == 0 { + // Nothing to match. + return -1 + } else if loc+len(pattern) <= len(text) && text[loc:loc+len(pattern)] == pattern { + // Perfect match at the perfect spot! (Includes case of null pattern) + return loc + } + // Do a fuzzy compare. + return dmp.MatchBitap(text, pattern, loc) +} + +// MatchBitap locates the best instance of 'pattern' in 'text' near 'loc' using the Bitap algorithm. +// Returns -1 if no match was found. +func (dmp *DiffMatchPatch) MatchBitap(text, pattern string, loc int) int { + // Initialise the alphabet. + s := dmp.MatchAlphabet(pattern) + + // Highest score beyond which we give up. + scoreThreshold := dmp.MatchThreshold + // Is there a nearby exact match? (speedup) + bestLoc := indexOf(text, pattern, loc) + if bestLoc != -1 { + scoreThreshold = math.Min(dmp.matchBitapScore(0, bestLoc, loc, + pattern), scoreThreshold) + // What about in the other direction? (speedup) + bestLoc = lastIndexOf(text, pattern, loc+len(pattern)) + if bestLoc != -1 { + scoreThreshold = math.Min(dmp.matchBitapScore(0, bestLoc, loc, + pattern), scoreThreshold) + } + } + + // Initialise the bit arrays. + matchmask := 1 << uint((len(pattern) - 1)) + bestLoc = -1 + + var binMin, binMid int + binMax := len(pattern) + len(text) + lastRd := []int{} + for d := 0; d < len(pattern); d++ { + // Scan for the best match; each iteration allows for one more error. Run a binary search to determine how far from 'loc' we can stray at this error level. + binMin = 0 + binMid = binMax + for binMin < binMid { + if dmp.matchBitapScore(d, loc+binMid, loc, pattern) <= scoreThreshold { + binMin = binMid + } else { + binMax = binMid + } + binMid = (binMax-binMin)/2 + binMin + } + // Use the result from this iteration as the maximum for the next. + binMax = binMid + start := int(math.Max(1, float64(loc-binMid+1))) + finish := int(math.Min(float64(loc+binMid), float64(len(text))) + float64(len(pattern))) + + rd := make([]int, finish+2) + rd[finish+1] = (1 << uint(d)) - 1 + + for j := finish; j >= start; j-- { + var charMatch int + if len(text) <= j-1 { + // Out of range. + charMatch = 0 + } else if _, ok := s[text[j-1]]; !ok { + charMatch = 0 + } else { + charMatch = s[text[j-1]] + } + + if d == 0 { + // First pass: exact match. + rd[j] = ((rd[j+1] << 1) | 1) & charMatch + } else { + // Subsequent passes: fuzzy match. + rd[j] = ((rd[j+1]<<1)|1)&charMatch | (((lastRd[j+1] | lastRd[j]) << 1) | 1) | lastRd[j+1] + } + if (rd[j] & matchmask) != 0 { + score := dmp.matchBitapScore(d, j-1, loc, pattern) + // This match will almost certainly be better than any existing match. But check anyway. + if score <= scoreThreshold { + // Told you so. + scoreThreshold = score + bestLoc = j - 1 + if bestLoc > loc { + // When passing loc, don't exceed our current distance from loc. + start = int(math.Max(1, float64(2*loc-bestLoc))) + } else { + // Already passed loc, downhill from here on in. + break + } + } + } + } + if dmp.matchBitapScore(d+1, loc, loc, pattern) > scoreThreshold { + // No hope for a (better) match at greater error levels. + break + } + lastRd = rd + } + return bestLoc +} + +// matchBitapScore computes and returns the score for a match with e errors and x location. +func (dmp *DiffMatchPatch) matchBitapScore(e, x, loc int, pattern string) float64 { + accuracy := float64(e) / float64(len(pattern)) + proximity := math.Abs(float64(loc - x)) + if dmp.MatchDistance == 0 { + // Dodge divide by zero error. + if proximity == 0 { + return accuracy + } + + return 1.0 + } + return accuracy + (proximity / float64(dmp.MatchDistance)) +} + +// MatchAlphabet initialises the alphabet for the Bitap algorithm. +func (dmp *DiffMatchPatch) MatchAlphabet(pattern string) map[byte]int { + s := map[byte]int{} + charPattern := []byte(pattern) + for _, c := range charPattern { + _, ok := s[c] + if !ok { + s[c] = 0 + } + } + i := 0 + + for _, c := range charPattern { + value := s[c] | int(uint(1)< y { + return x + } + return y +} diff --git a/vendor/github.com/sergi/go-diff/diffmatchpatch/operation_string.go b/vendor/github.com/sergi/go-diff/diffmatchpatch/operation_string.go new file mode 100644 index 000000000..533ec0da7 --- /dev/null +++ b/vendor/github.com/sergi/go-diff/diffmatchpatch/operation_string.go @@ -0,0 +1,17 @@ +// Code generated by "stringer -type=Operation -trimprefix=Diff"; DO NOT EDIT. + +package diffmatchpatch + +import "fmt" + +const _Operation_name = "DeleteEqualInsert" + +var _Operation_index = [...]uint8{0, 6, 11, 17} + +func (i Operation) String() string { + i -= -1 + if i < 0 || i >= Operation(len(_Operation_index)-1) { + return fmt.Sprintf("Operation(%d)", i+-1) + } + return _Operation_name[_Operation_index[i]:_Operation_index[i+1]] +} diff --git a/vendor/github.com/sergi/go-diff/diffmatchpatch/patch.go b/vendor/github.com/sergi/go-diff/diffmatchpatch/patch.go new file mode 100644 index 000000000..0dbe3bdd7 --- /dev/null +++ b/vendor/github.com/sergi/go-diff/diffmatchpatch/patch.go @@ -0,0 +1,556 @@ +// Copyright (c) 2012-2016 The go-diff authors. All rights reserved. +// https://github.com/sergi/go-diff +// See the included LICENSE file for license details. +// +// go-diff is a Go implementation of Google's Diff, Match, and Patch library +// Original library is Copyright (c) 2006 Google Inc. +// http://code.google.com/p/google-diff-match-patch/ + +package diffmatchpatch + +import ( + "bytes" + "errors" + "math" + "net/url" + "regexp" + "strconv" + "strings" +) + +// Patch represents one patch operation. +type Patch struct { + diffs []Diff + Start1 int + Start2 int + Length1 int + Length2 int +} + +// String emulates GNU diff's format. +// Header: @@ -382,8 +481,9 @@ +// Indices are printed as 1-based, not 0-based. +func (p *Patch) String() string { + var coords1, coords2 string + + if p.Length1 == 0 { + coords1 = strconv.Itoa(p.Start1) + ",0" + } else if p.Length1 == 1 { + coords1 = strconv.Itoa(p.Start1 + 1) + } else { + coords1 = strconv.Itoa(p.Start1+1) + "," + strconv.Itoa(p.Length1) + } + + if p.Length2 == 0 { + coords2 = strconv.Itoa(p.Start2) + ",0" + } else if p.Length2 == 1 { + coords2 = strconv.Itoa(p.Start2 + 1) + } else { + coords2 = strconv.Itoa(p.Start2+1) + "," + strconv.Itoa(p.Length2) + } + + var text bytes.Buffer + _, _ = text.WriteString("@@ -" + coords1 + " +" + coords2 + " @@\n") + + // Escape the body of the patch with %xx notation. + for _, aDiff := range p.diffs { + switch aDiff.Type { + case DiffInsert: + _, _ = text.WriteString("+") + case DiffDelete: + _, _ = text.WriteString("-") + case DiffEqual: + _, _ = text.WriteString(" ") + } + + _, _ = text.WriteString(strings.Replace(url.QueryEscape(aDiff.Text), "+", " ", -1)) + _, _ = text.WriteString("\n") + } + + return unescaper.Replace(text.String()) +} + +// PatchAddContext increases the context until it is unique, but doesn't let the pattern expand beyond MatchMaxBits. +func (dmp *DiffMatchPatch) PatchAddContext(patch Patch, text string) Patch { + if len(text) == 0 { + return patch + } + + pattern := text[patch.Start2 : patch.Start2+patch.Length1] + padding := 0 + + // Look for the first and last matches of pattern in text. If two different matches are found, increase the pattern length. + for strings.Index(text, pattern) != strings.LastIndex(text, pattern) && + len(pattern) < dmp.MatchMaxBits-2*dmp.PatchMargin { + padding += dmp.PatchMargin + maxStart := max(0, patch.Start2-padding) + minEnd := min(len(text), patch.Start2+patch.Length1+padding) + pattern = text[maxStart:minEnd] + } + // Add one chunk for good luck. + padding += dmp.PatchMargin + + // Add the prefix. + prefix := text[max(0, patch.Start2-padding):patch.Start2] + if len(prefix) != 0 { + patch.diffs = append([]Diff{Diff{DiffEqual, prefix}}, patch.diffs...) + } + // Add the suffix. + suffix := text[patch.Start2+patch.Length1 : min(len(text), patch.Start2+patch.Length1+padding)] + if len(suffix) != 0 { + patch.diffs = append(patch.diffs, Diff{DiffEqual, suffix}) + } + + // Roll back the start points. + patch.Start1 -= len(prefix) + patch.Start2 -= len(prefix) + // Extend the lengths. + patch.Length1 += len(prefix) + len(suffix) + patch.Length2 += len(prefix) + len(suffix) + + return patch +} + +// PatchMake computes a list of patches. +func (dmp *DiffMatchPatch) PatchMake(opt ...interface{}) []Patch { + if len(opt) == 1 { + diffs, _ := opt[0].([]Diff) + text1 := dmp.DiffText1(diffs) + return dmp.PatchMake(text1, diffs) + } else if len(opt) == 2 { + text1 := opt[0].(string) + switch t := opt[1].(type) { + case string: + diffs := dmp.DiffMain(text1, t, true) + if len(diffs) > 2 { + diffs = dmp.DiffCleanupSemantic(diffs) + diffs = dmp.DiffCleanupEfficiency(diffs) + } + return dmp.PatchMake(text1, diffs) + case []Diff: + return dmp.patchMake2(text1, t) + } + } else if len(opt) == 3 { + return dmp.PatchMake(opt[0], opt[2]) + } + return []Patch{} +} + +// patchMake2 computes a list of patches to turn text1 into text2. +// text2 is not provided, diffs are the delta between text1 and text2. +func (dmp *DiffMatchPatch) patchMake2(text1 string, diffs []Diff) []Patch { + // Check for null inputs not needed since null can't be passed in C#. + patches := []Patch{} + if len(diffs) == 0 { + return patches // Get rid of the null case. + } + + patch := Patch{} + charCount1 := 0 // Number of characters into the text1 string. + charCount2 := 0 // Number of characters into the text2 string. + // Start with text1 (prepatchText) and apply the diffs until we arrive at text2 (postpatchText). We recreate the patches one by one to determine context info. + prepatchText := text1 + postpatchText := text1 + + for i, aDiff := range diffs { + if len(patch.diffs) == 0 && aDiff.Type != DiffEqual { + // A new patch starts here. + patch.Start1 = charCount1 + patch.Start2 = charCount2 + } + + switch aDiff.Type { + case DiffInsert: + patch.diffs = append(patch.diffs, aDiff) + patch.Length2 += len(aDiff.Text) + postpatchText = postpatchText[:charCount2] + + aDiff.Text + postpatchText[charCount2:] + case DiffDelete: + patch.Length1 += len(aDiff.Text) + patch.diffs = append(patch.diffs, aDiff) + postpatchText = postpatchText[:charCount2] + postpatchText[charCount2+len(aDiff.Text):] + case DiffEqual: + if len(aDiff.Text) <= 2*dmp.PatchMargin && + len(patch.diffs) != 0 && i != len(diffs)-1 { + // Small equality inside a patch. + patch.diffs = append(patch.diffs, aDiff) + patch.Length1 += len(aDiff.Text) + patch.Length2 += len(aDiff.Text) + } + if len(aDiff.Text) >= 2*dmp.PatchMargin { + // Time for a new patch. + if len(patch.diffs) != 0 { + patch = dmp.PatchAddContext(patch, prepatchText) + patches = append(patches, patch) + patch = Patch{} + // Unlike Unidiff, our patch lists have a rolling context. http://code.google.com/p/google-diff-match-patch/wiki/Unidiff Update prepatch text & pos to reflect the application of the just completed patch. + prepatchText = postpatchText + charCount1 = charCount2 + } + } + } + + // Update the current character count. + if aDiff.Type != DiffInsert { + charCount1 += len(aDiff.Text) + } + if aDiff.Type != DiffDelete { + charCount2 += len(aDiff.Text) + } + } + + // Pick up the leftover patch if not empty. + if len(patch.diffs) != 0 { + patch = dmp.PatchAddContext(patch, prepatchText) + patches = append(patches, patch) + } + + return patches +} + +// PatchDeepCopy returns an array that is identical to a given an array of patches. +func (dmp *DiffMatchPatch) PatchDeepCopy(patches []Patch) []Patch { + patchesCopy := []Patch{} + for _, aPatch := range patches { + patchCopy := Patch{} + for _, aDiff := range aPatch.diffs { + patchCopy.diffs = append(patchCopy.diffs, Diff{ + aDiff.Type, + aDiff.Text, + }) + } + patchCopy.Start1 = aPatch.Start1 + patchCopy.Start2 = aPatch.Start2 + patchCopy.Length1 = aPatch.Length1 + patchCopy.Length2 = aPatch.Length2 + patchesCopy = append(patchesCopy, patchCopy) + } + return patchesCopy +} + +// PatchApply merges a set of patches onto the text. Returns a patched text, as well as an array of true/false values indicating which patches were applied. +func (dmp *DiffMatchPatch) PatchApply(patches []Patch, text string) (string, []bool) { + if len(patches) == 0 { + return text, []bool{} + } + + // Deep copy the patches so that no changes are made to originals. + patches = dmp.PatchDeepCopy(patches) + + nullPadding := dmp.PatchAddPadding(patches) + text = nullPadding + text + nullPadding + patches = dmp.PatchSplitMax(patches) + + x := 0 + // delta keeps track of the offset between the expected and actual location of the previous patch. If there are patches expected at positions 10 and 20, but the first patch was found at 12, delta is 2 and the second patch has an effective expected position of 22. + delta := 0 + results := make([]bool, len(patches)) + for _, aPatch := range patches { + expectedLoc := aPatch.Start2 + delta + text1 := dmp.DiffText1(aPatch.diffs) + var startLoc int + endLoc := -1 + if len(text1) > dmp.MatchMaxBits { + // PatchSplitMax will only provide an oversized pattern in the case of a monster delete. + startLoc = dmp.MatchMain(text, text1[:dmp.MatchMaxBits], expectedLoc) + if startLoc != -1 { + endLoc = dmp.MatchMain(text, + text1[len(text1)-dmp.MatchMaxBits:], expectedLoc+len(text1)-dmp.MatchMaxBits) + if endLoc == -1 || startLoc >= endLoc { + // Can't find valid trailing context. Drop this patch. + startLoc = -1 + } + } + } else { + startLoc = dmp.MatchMain(text, text1, expectedLoc) + } + if startLoc == -1 { + // No match found. :( + results[x] = false + // Subtract the delta for this failed patch from subsequent patches. + delta -= aPatch.Length2 - aPatch.Length1 + } else { + // Found a match. :) + results[x] = true + delta = startLoc - expectedLoc + var text2 string + if endLoc == -1 { + text2 = text[startLoc:int(math.Min(float64(startLoc+len(text1)), float64(len(text))))] + } else { + text2 = text[startLoc:int(math.Min(float64(endLoc+dmp.MatchMaxBits), float64(len(text))))] + } + if text1 == text2 { + // Perfect match, just shove the Replacement text in. + text = text[:startLoc] + dmp.DiffText2(aPatch.diffs) + text[startLoc+len(text1):] + } else { + // Imperfect match. Run a diff to get a framework of equivalent indices. + diffs := dmp.DiffMain(text1, text2, false) + if len(text1) > dmp.MatchMaxBits && float64(dmp.DiffLevenshtein(diffs))/float64(len(text1)) > dmp.PatchDeleteThreshold { + // The end points match, but the content is unacceptably bad. + results[x] = false + } else { + diffs = dmp.DiffCleanupSemanticLossless(diffs) + index1 := 0 + for _, aDiff := range aPatch.diffs { + if aDiff.Type != DiffEqual { + index2 := dmp.DiffXIndex(diffs, index1) + if aDiff.Type == DiffInsert { + // Insertion + text = text[:startLoc+index2] + aDiff.Text + text[startLoc+index2:] + } else if aDiff.Type == DiffDelete { + // Deletion + startIndex := startLoc + index2 + text = text[:startIndex] + + text[startIndex+dmp.DiffXIndex(diffs, index1+len(aDiff.Text))-index2:] + } + } + if aDiff.Type != DiffDelete { + index1 += len(aDiff.Text) + } + } + } + } + } + x++ + } + // Strip the padding off. + text = text[len(nullPadding) : len(nullPadding)+(len(text)-2*len(nullPadding))] + return text, results +} + +// PatchAddPadding adds some padding on text start and end so that edges can match something. +// Intended to be called only from within patchApply. +func (dmp *DiffMatchPatch) PatchAddPadding(patches []Patch) string { + paddingLength := dmp.PatchMargin + nullPadding := "" + for x := 1; x <= paddingLength; x++ { + nullPadding += string(rune(x)) + } + + // Bump all the patches forward. + for i := range patches { + patches[i].Start1 += paddingLength + patches[i].Start2 += paddingLength + } + + // Add some padding on start of first diff. + if len(patches[0].diffs) == 0 || patches[0].diffs[0].Type != DiffEqual { + // Add nullPadding equality. + patches[0].diffs = append([]Diff{Diff{DiffEqual, nullPadding}}, patches[0].diffs...) + patches[0].Start1 -= paddingLength // Should be 0. + patches[0].Start2 -= paddingLength // Should be 0. + patches[0].Length1 += paddingLength + patches[0].Length2 += paddingLength + } else if paddingLength > len(patches[0].diffs[0].Text) { + // Grow first equality. + extraLength := paddingLength - len(patches[0].diffs[0].Text) + patches[0].diffs[0].Text = nullPadding[len(patches[0].diffs[0].Text):] + patches[0].diffs[0].Text + patches[0].Start1 -= extraLength + patches[0].Start2 -= extraLength + patches[0].Length1 += extraLength + patches[0].Length2 += extraLength + } + + // Add some padding on end of last diff. + last := len(patches) - 1 + if len(patches[last].diffs) == 0 || patches[last].diffs[len(patches[last].diffs)-1].Type != DiffEqual { + // Add nullPadding equality. + patches[last].diffs = append(patches[last].diffs, Diff{DiffEqual, nullPadding}) + patches[last].Length1 += paddingLength + patches[last].Length2 += paddingLength + } else if paddingLength > len(patches[last].diffs[len(patches[last].diffs)-1].Text) { + // Grow last equality. + lastDiff := patches[last].diffs[len(patches[last].diffs)-1] + extraLength := paddingLength - len(lastDiff.Text) + patches[last].diffs[len(patches[last].diffs)-1].Text += nullPadding[:extraLength] + patches[last].Length1 += extraLength + patches[last].Length2 += extraLength + } + + return nullPadding +} + +// PatchSplitMax looks through the patches and breaks up any which are longer than the maximum limit of the match algorithm. +// Intended to be called only from within patchApply. +func (dmp *DiffMatchPatch) PatchSplitMax(patches []Patch) []Patch { + patchSize := dmp.MatchMaxBits + for x := 0; x < len(patches); x++ { + if patches[x].Length1 <= patchSize { + continue + } + bigpatch := patches[x] + // Remove the big old patch. + patches = append(patches[:x], patches[x+1:]...) + x-- + + Start1 := bigpatch.Start1 + Start2 := bigpatch.Start2 + precontext := "" + for len(bigpatch.diffs) != 0 { + // Create one of several smaller patches. + patch := Patch{} + empty := true + patch.Start1 = Start1 - len(precontext) + patch.Start2 = Start2 - len(precontext) + if len(precontext) != 0 { + patch.Length1 = len(precontext) + patch.Length2 = len(precontext) + patch.diffs = append(patch.diffs, Diff{DiffEqual, precontext}) + } + for len(bigpatch.diffs) != 0 && patch.Length1 < patchSize-dmp.PatchMargin { + diffType := bigpatch.diffs[0].Type + diffText := bigpatch.diffs[0].Text + if diffType == DiffInsert { + // Insertions are harmless. + patch.Length2 += len(diffText) + Start2 += len(diffText) + patch.diffs = append(patch.diffs, bigpatch.diffs[0]) + bigpatch.diffs = bigpatch.diffs[1:] + empty = false + } else if diffType == DiffDelete && len(patch.diffs) == 1 && patch.diffs[0].Type == DiffEqual && len(diffText) > 2*patchSize { + // This is a large deletion. Let it pass in one chunk. + patch.Length1 += len(diffText) + Start1 += len(diffText) + empty = false + patch.diffs = append(patch.diffs, Diff{diffType, diffText}) + bigpatch.diffs = bigpatch.diffs[1:] + } else { + // Deletion or equality. Only take as much as we can stomach. + diffText = diffText[:min(len(diffText), patchSize-patch.Length1-dmp.PatchMargin)] + + patch.Length1 += len(diffText) + Start1 += len(diffText) + if diffType == DiffEqual { + patch.Length2 += len(diffText) + Start2 += len(diffText) + } else { + empty = false + } + patch.diffs = append(patch.diffs, Diff{diffType, diffText}) + if diffText == bigpatch.diffs[0].Text { + bigpatch.diffs = bigpatch.diffs[1:] + } else { + bigpatch.diffs[0].Text = + bigpatch.diffs[0].Text[len(diffText):] + } + } + } + // Compute the head context for the next patch. + precontext = dmp.DiffText2(patch.diffs) + precontext = precontext[max(0, len(precontext)-dmp.PatchMargin):] + + postcontext := "" + // Append the end context for this patch. + if len(dmp.DiffText1(bigpatch.diffs)) > dmp.PatchMargin { + postcontext = dmp.DiffText1(bigpatch.diffs)[:dmp.PatchMargin] + } else { + postcontext = dmp.DiffText1(bigpatch.diffs) + } + + if len(postcontext) != 0 { + patch.Length1 += len(postcontext) + patch.Length2 += len(postcontext) + if len(patch.diffs) != 0 && patch.diffs[len(patch.diffs)-1].Type == DiffEqual { + patch.diffs[len(patch.diffs)-1].Text += postcontext + } else { + patch.diffs = append(patch.diffs, Diff{DiffEqual, postcontext}) + } + } + if !empty { + x++ + patches = append(patches[:x], append([]Patch{patch}, patches[x:]...)...) + } + } + } + return patches +} + +// PatchToText takes a list of patches and returns a textual representation. +func (dmp *DiffMatchPatch) PatchToText(patches []Patch) string { + var text bytes.Buffer + for _, aPatch := range patches { + _, _ = text.WriteString(aPatch.String()) + } + return text.String() +} + +// PatchFromText parses a textual representation of patches and returns a List of Patch objects. +func (dmp *DiffMatchPatch) PatchFromText(textline string) ([]Patch, error) { + patches := []Patch{} + if len(textline) == 0 { + return patches, nil + } + text := strings.Split(textline, "\n") + textPointer := 0 + patchHeader := regexp.MustCompile("^@@ -(\\d+),?(\\d*) \\+(\\d+),?(\\d*) @@$") + + var patch Patch + var sign uint8 + var line string + for textPointer < len(text) { + + if !patchHeader.MatchString(text[textPointer]) { + return patches, errors.New("Invalid patch string: " + text[textPointer]) + } + + patch = Patch{} + m := patchHeader.FindStringSubmatch(text[textPointer]) + + patch.Start1, _ = strconv.Atoi(m[1]) + if len(m[2]) == 0 { + patch.Start1-- + patch.Length1 = 1 + } else if m[2] == "0" { + patch.Length1 = 0 + } else { + patch.Start1-- + patch.Length1, _ = strconv.Atoi(m[2]) + } + + patch.Start2, _ = strconv.Atoi(m[3]) + + if len(m[4]) == 0 { + patch.Start2-- + patch.Length2 = 1 + } else if m[4] == "0" { + patch.Length2 = 0 + } else { + patch.Start2-- + patch.Length2, _ = strconv.Atoi(m[4]) + } + textPointer++ + + for textPointer < len(text) { + if len(text[textPointer]) > 0 { + sign = text[textPointer][0] + } else { + textPointer++ + continue + } + + line = text[textPointer][1:] + line = strings.Replace(line, "+", "%2b", -1) + line, _ = url.QueryUnescape(line) + if sign == '-' { + // Deletion. + patch.diffs = append(patch.diffs, Diff{DiffDelete, line}) + } else if sign == '+' { + // Insertion. + patch.diffs = append(patch.diffs, Diff{DiffInsert, line}) + } else if sign == ' ' { + // Minor equality. + patch.diffs = append(patch.diffs, Diff{DiffEqual, line}) + } else if sign == '@' { + // Start of next patch. + break + } else { + // WTF? + return patches, errors.New("Invalid patch mode '" + string(sign) + "' in: " + string(line)) + } + textPointer++ + } + + patches = append(patches, patch) + } + return patches, nil +} diff --git a/vendor/github.com/sergi/go-diff/diffmatchpatch/stringutil.go b/vendor/github.com/sergi/go-diff/diffmatchpatch/stringutil.go new file mode 100644 index 000000000..44c435954 --- /dev/null +++ b/vendor/github.com/sergi/go-diff/diffmatchpatch/stringutil.go @@ -0,0 +1,106 @@ +// Copyright (c) 2012-2016 The go-diff authors. All rights reserved. +// https://github.com/sergi/go-diff +// See the included LICENSE file for license details. +// +// go-diff is a Go implementation of Google's Diff, Match, and Patch library +// Original library is Copyright (c) 2006 Google Inc. +// http://code.google.com/p/google-diff-match-patch/ + +package diffmatchpatch + +import ( + "strconv" + "strings" + "unicode/utf8" +) + +// unescaper unescapes selected chars for compatibility with JavaScript's encodeURI. +// In speed critical applications this could be dropped since the receiving application will certainly decode these fine. Note that this function is case-sensitive. Thus "%3F" would not be unescaped. But this is ok because it is only called with the output of HttpUtility.UrlEncode which returns lowercase hex. Example: "%3f" -> "?", "%24" -> "$", etc. +var unescaper = strings.NewReplacer( + "%21", "!", "%7E", "~", "%27", "'", + "%28", "(", "%29", ")", "%3B", ";", + "%2F", "/", "%3F", "?", "%3A", ":", + "%40", "@", "%26", "&", "%3D", "=", + "%2B", "+", "%24", "$", "%2C", ",", "%23", "#", "%2A", "*") + +// indexOf returns the first index of pattern in str, starting at str[i]. +func indexOf(str string, pattern string, i int) int { + if i > len(str)-1 { + return -1 + } + if i <= 0 { + return strings.Index(str, pattern) + } + ind := strings.Index(str[i:], pattern) + if ind == -1 { + return -1 + } + return ind + i +} + +// lastIndexOf returns the last index of pattern in str, starting at str[i]. +func lastIndexOf(str string, pattern string, i int) int { + if i < 0 { + return -1 + } + if i >= len(str) { + return strings.LastIndex(str, pattern) + } + _, size := utf8.DecodeRuneInString(str[i:]) + return strings.LastIndex(str[:i+size], pattern) +} + +// runesIndexOf returns the index of pattern in target, starting at target[i]. +func runesIndexOf(target, pattern []rune, i int) int { + if i > len(target)-1 { + return -1 + } + if i <= 0 { + return runesIndex(target, pattern) + } + ind := runesIndex(target[i:], pattern) + if ind == -1 { + return -1 + } + return ind + i +} + +func runesEqual(r1, r2 []rune) bool { + if len(r1) != len(r2) { + return false + } + for i, c := range r1 { + if c != r2[i] { + return false + } + } + return true +} + +// runesIndex is the equivalent of strings.Index for rune slices. +func runesIndex(r1, r2 []rune) int { + last := len(r1) - len(r2) + for i := 0; i <= last; i++ { + if runesEqual(r1[i:i+len(r2)], r2) { + return i + } + } + return -1 +} + +func intArrayToString(ns []uint32) string { + if len(ns) == 0 { + return "" + } + + indexSeparator := IndexSeparator[0] + + // Appr. 3 chars per num plus the comma. + b := []byte{} + for _, n := range ns { + b = strconv.AppendInt(b, int64(n), 10) + b = append(b, indexSeparator) + } + b = b[:len(b)-1] + return string(b) +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 482041da7..14fb5d834 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -886,7 +886,7 @@ github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1 # github.com/metal3-io/baremetal-operator/pkg/hardwareutils v0.5.1 ## explicit; go 1.20 github.com/metal3-io/baremetal-operator/pkg/hardwareutils/bmc -# github.com/metal3-io/cluster-api-provider-metal3/api v1.10.1 +# github.com/metal3-io/cluster-api-provider-metal3/api v1.10.1 => github.com/metal3-io/cluster-api-provider-metal3/api v1.10.1 ## explicit; go 1.23.7 github.com/metal3-io/cluster-api-provider-metal3/api/v1beta1 # github.com/metal3-io/ip-address-manager/api v1.10.1 @@ -1114,7 +1114,7 @@ github.com/openshift/cluster-api-actuator-pkg/testutils/resourcebuilder/config/v github.com/openshift/cluster-api-actuator-pkg/testutils/resourcebuilder/core/v1 github.com/openshift/cluster-api-actuator-pkg/testutils/resourcebuilder/machine/v1 github.com/openshift/cluster-api-actuator-pkg/testutils/resourcebuilder/machine/v1beta1 -# github.com/openshift/cluster-api-provider-baremetal v0.0.0-20250619124612-fb678fec5f7e +# github.com/openshift/cluster-api-provider-baremetal v0.0.0-20250930083252-4e6a729c5ac8 ## explicit; go 1.23.0 github.com/openshift/cluster-api-provider-baremetal/pkg/apis/baremetal/v1alpha1 # github.com/openshift/cluster-autoscaler-operator v0.0.1-0.20250702183526-4eb64d553940 @@ -1263,6 +1263,7 @@ github.com/securego/gosec/v2/issue github.com/securego/gosec/v2/rules # github.com/sergi/go-diff v1.3.1 ## explicit; go 1.12 +github.com/sergi/go-diff/diffmatchpatch # github.com/sirupsen/logrus v1.9.3 ## explicit; go 1.13 github.com/sirupsen/logrus @@ -2837,8 +2838,8 @@ sigs.k8s.io/kustomize/api/provider sigs.k8s.io/kustomize/api/resmap sigs.k8s.io/kustomize/api/resource sigs.k8s.io/kustomize/api/types -# sigs.k8s.io/kustomize/cmd/config v0.14.0 -## explicit; go 1.21 +# sigs.k8s.io/kustomize/cmd/config v0.19.0 +## explicit; go 1.22.7 sigs.k8s.io/kustomize/cmd/config/completion sigs.k8s.io/kustomize/cmd/config/configcobra sigs.k8s.io/kustomize/cmd/config/ext @@ -2848,8 +2849,8 @@ sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/api sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/tutorials sigs.k8s.io/kustomize/cmd/config/runner -# sigs.k8s.io/kustomize/kustomize/v5 v5.4.1 -## explicit; go 1.21 +# sigs.k8s.io/kustomize/kustomize/v5 v5.6.0 +## explicit; go 1.22.7 sigs.k8s.io/kustomize/kustomize/v5 sigs.k8s.io/kustomize/kustomize/v5/commands sigs.k8s.io/kustomize/kustomize/v5/commands/build @@ -2870,6 +2871,7 @@ sigs.k8s.io/kustomize/kustomize/v5/commands/version # sigs.k8s.io/kustomize/kyaml v0.19.0 ## explicit; go 1.22.7 sigs.k8s.io/kustomize/kyaml/comments +sigs.k8s.io/kustomize/kyaml/copyutil sigs.k8s.io/kustomize/kyaml/errors sigs.k8s.io/kustomize/kyaml/ext sigs.k8s.io/kustomize/kyaml/fieldmeta @@ -2920,3 +2922,4 @@ sigs.k8s.io/yaml/goyaml.v2 sigs.k8s.io/yaml/goyaml.v3 # github.com/metal3-io/baremetal-operator => github.com/metal3-io/baremetal-operator v0.5.1 # sigs.k8s.io/cluster-api => sigs.k8s.io/cluster-api v1.10.4 +# github.com/metal3-io/cluster-api-provider-metal3/api => github.com/metal3-io/cluster-api-provider-metal3/api v1.10.1 diff --git a/vendor/sigs.k8s.io/kustomize/cmd/config/internal/commands/grep.go b/vendor/sigs.k8s.io/kustomize/cmd/config/internal/commands/grep.go index 54659f510..9e2f4b47d 100644 --- a/vendor/sigs.k8s.io/kustomize/cmd/config/internal/commands/grep.go +++ b/vendor/sigs.k8s.io/kustomize/cmd/config/internal/commands/grep.go @@ -54,6 +54,9 @@ type GrepRunner struct { } func (r *GrepRunner) preRunE(c *cobra.Command, args []string) error { + if len(args) == 0 { + return fmt.Errorf("missing required argument: QUERY") + } r.GrepFilter.Compare = func(a, b string) (int, error) { qa, err := resource.ParseQuantity(a) if err != nil { diff --git a/vendor/sigs.k8s.io/kustomize/cmd/config/internal/commands/run-fns.go b/vendor/sigs.k8s.io/kustomize/cmd/config/internal/commands/run-fns.go index ee9446881..4dfdedb54 100644 --- a/vendor/sigs.k8s.io/kustomize/cmd/config/internal/commands/run-fns.go +++ b/vendor/sigs.k8s.io/kustomize/cmd/config/internal/commands/run-fns.go @@ -20,7 +20,7 @@ import ( "sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands" ) -// GetCatRunner returns a RunFnRunner. +// GetRunFnRunner returns a RunFnRunner. func GetRunFnRunner(name string) *RunFnRunner { r := &RunFnRunner{} c := &cobra.Command{ @@ -51,14 +51,6 @@ func GetRunFnRunner(name string) *RunFnRunner { "enable support for exec functions -- note: exec functions run arbitrary code -- do not use for untrusted configs!!! (Alpha)") r.Command.Flags().StringVar( &r.ExecPath, "exec-path", "", "run an executable as a function. (Alpha)") - r.Command.Flags().BoolVar( - &r.EnableStar, "enable-star", false, "enable support for starlark functions. (Alpha)") - r.Command.Flags().StringVar( - &r.StarPath, "star-path", "", "run a starlark script as a function. (Alpha)") - r.Command.Flags().StringVar( - &r.StarURL, "star-url", "", "run a starlark script as a function. (Alpha)") - r.Command.Flags().StringVar( - &r.StarName, "star-name", "", "name of starlark program. (Alpha)") r.Command.Flags().StringVar( &r.ResultsDir, "results-dir", "", "write function results to this dir") @@ -91,7 +83,6 @@ type RunFnRunner struct { GlobalScope bool FnPaths []string Image string - EnableStar bool StarPath string StarURL string StarName string @@ -128,8 +119,6 @@ func (r *RunFnRunner) getContainerFunctions(dataItems []string) ( switch { case r.Image != "": fnAnnotation, err = fnAnnotationForImage(r.Image, r.Network) - case r.EnableStar && (r.StarPath != "" || r.StarURL != ""): - fnAnnotation, err = fnAnnotationForStar(r.StarPath, r.StarURL, r.StarName) case r.EnableExec && r.ExecPath != "": fnAnnotation, err = fnAnnotationForExec(r.ExecPath) } @@ -218,37 +207,6 @@ func fnAnnotationForExec(path string) (*yaml.RNode, error) { return fn, nil } -func fnAnnotationForStar(path string, url string, name string) (*yaml.RNode, error) { - fn, err := yaml.Parse(`starlark: {}`) - if err != nil { - return nil, errors.Wrap(err) - } - - if path != "" { - err = fn.PipeE( - yaml.Lookup("starlark"), - yaml.SetField("path", yaml.NewScalarRNode(path))) - if err != nil { - return nil, errors.Wrap(err) - } - } - if url != "" { - err = fn.PipeE( - yaml.Lookup("starlark"), - yaml.SetField("url", yaml.NewScalarRNode(url))) - if err != nil { - return nil, errors.Wrap(err) - } - } - err = fn.PipeE( - yaml.Lookup("starlark"), - yaml.SetField("name", yaml.NewScalarRNode(name))) - if err != nil { - return nil, errors.Wrap(err) - } - return fn, nil -} - func fnAnnotationForImage(image string, enableNetwork bool) (*yaml.RNode, error) { fn, err := yaml.Parse(`container: {}`) if err != nil { @@ -286,16 +244,11 @@ func toStorageMounts(mounts []string) []runtimeutil.StorageMount { } func (r *RunFnRunner) preRunE(c *cobra.Command, args []string) error { - if !r.EnableStar && (r.StarPath != "" || r.StarURL != "") { - return errors.Errorf("must specify --enable-star with --star-path and --star-url") - } - if !r.EnableExec && r.ExecPath != "" { return errors.Errorf("must specify --enable-exec with --exec-path") } - if c.ArgsLenAtDash() >= 0 && r.Image == "" && - !(r.EnableStar && (r.StarPath != "" || r.StarURL != "")) && !(r.EnableExec && r.ExecPath != "") { + if c.ArgsLenAtDash() >= 0 && r.Image == "" && !(r.EnableExec && r.ExecPath != "") { return errors.Errorf("must specify --image") } @@ -346,7 +299,6 @@ func (r *RunFnRunner) preRunE(c *cobra.Command, args []string) error { Input: input, Path: path, Network: r.Network, - EnableStarlark: r.EnableStar, EnableExec: r.EnableExec, StorageMounts: storageMounts, ResultsDir: r.ResultsDir, diff --git a/vendor/sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/api/docs.go b/vendor/sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/api/docs.go index ba03f8ddf..0776145c8 100644 --- a/vendor/sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/api/docs.go +++ b/vendor/sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/api/docs.go @@ -144,7 +144,7 @@ The script wraps itself using ` + "`" + `config run wrap -- $0` + "`" + ` which ` + "`" + `Dockerfile` + "`" + ` installs ` + "`" + `kustomize fn` + "`" + ` and copies the script into the container image. - FROM golang:1.21-bullseye + FROM public.ecr.aws/docker/library/golang:1.22.7-bullseye RUN go get sigs.k8s.io/kustomize/cmd/config RUN mv /go/bin/config /usr/bin/config COPY nginx-template.sh /usr/bin/nginx-template.sh diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/build/build.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/build/build.go index 96dcc7260..21ecaeb6f 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/build/build.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/build/build.go @@ -30,6 +30,7 @@ var theFlags struct { helmCommand string helmApiVersions []string helmKubeVersion string + helmDebug bool loadRestrictor string reorderOutput string fnOptions types.FnPluginLoadingOptions @@ -163,6 +164,7 @@ func HonorKustomizeFlags(kOpts *krusty.Options, flags *flag.FlagSet) *krusty.Opt kOpts.PluginConfig.HelmConfig.Command = theFlags.helmCommand kOpts.PluginConfig.HelmConfig.ApiVersions = theFlags.helmApiVersions kOpts.PluginConfig.HelmConfig.KubeVersion = theFlags.helmKubeVersion + kOpts.PluginConfig.HelmConfig.Debug = theFlags.helmDebug kOpts.AddManagedbyLabel = isManagedByLabelEnabled() return kOpts } diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/build/flagenablehelm.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/build/flagenablehelm.go index a379d180a..d7d02d602 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/build/flagenablehelm.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/build/flagenablehelm.go @@ -31,4 +31,9 @@ func AddFlagEnableHelm(set *pflag.FlagSet) { "helm-kube-version", "", // default "Kubernetes version used by Helm for Capabilities.KubeVersion") + set.BoolVar( + &theFlags.helmDebug, + "helm-debug", + false, + "Enable debug output from the Helm chart inflator generator.") } diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/build/flagsforfunctions.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/build/flagsforfunctions.go index 8b22f5a20..9f4a3d231 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/build/flagsforfunctions.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/build/flagsforfunctions.go @@ -30,7 +30,4 @@ func AddFunctionAlphaEnablementFlags(set *pflag.FlagSet) { &theFlags.fnOptions.EnableExec, "enable-exec", false, "enable support for exec functions (raw executables); "+ "do not use for untrusted configs! (Alpha)") - set.BoolVar( - &theFlags.fnOptions.EnableStar, "enable-star", false, - "enable support for starlark functions. (Alpha)") } diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/create/create.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/create/create.go index 81a05dbab..6b867c908 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/create/create.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/create/create.go @@ -7,6 +7,7 @@ import ( "fmt" "os" "path/filepath" + "slices" "strings" "github.com/spf13/cobra" @@ -113,7 +114,7 @@ func runCreate(opts createFlags, fSys filesys.FileSystem, rf *resource.Factory) return err } for _, resource := range detected { - if kustfile.StringInSlice(resource, resources) { + if slices.Contains(resources, resource) { continue } resources = append(resources, resource) diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addbase.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addbase.go index 01a422e5a..b33f128df 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addbase.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addbase.go @@ -8,6 +8,8 @@ import ( "fmt" "strings" + "slices" + "github.com/spf13/cobra" "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile" "sigs.k8s.io/kustomize/kyaml/filesys" @@ -64,7 +66,7 @@ func (o *addBaseOptions) RunAddBase(fSys filesys.FileSystem) error { if !fSys.Exists(path) { return errors.New(path + " does not exist") } - if kustfile.StringInSlice(path, m.Resources) { + if slices.Contains(m.Resources, path) { return fmt.Errorf("base %s already in kustomization file", path) } m.Resources = append(m.Resources, path) diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addbuildmetadata.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addbuildmetadata.go index e4aa86ca3..d8af37842 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addbuildmetadata.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addbuildmetadata.go @@ -5,6 +5,7 @@ package add import ( "fmt" + "slices" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile" @@ -60,7 +61,7 @@ func (o *addBuildMetadataOptions) RunAddBuildMetadata(fSys filesys.FileSystem) e return err } for _, opt := range o.buildMetadataOptions { - if kustfile.StringInSlice(opt, m.BuildMetadata) { + if slices.Contains(m.BuildMetadata, opt) { return fmt.Errorf("buildMetadata option %s already in kustomization file", opt) } m.BuildMetadata = append(m.BuildMetadata, opt) diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addcomponent.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addcomponent.go index c0f728f71..a5078ae73 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addcomponent.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addcomponent.go @@ -6,6 +6,7 @@ package add import ( "errors" "log" + "slices" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/api/pkg/loader" @@ -69,7 +70,7 @@ func (o *addComponentOptions) RunAddComponent(fSys filesys.FileSystem) error { for _, component := range components { if mf.GetPath() != component { - if kustfile.StringInSlice(component, m.Components) { + if slices.Contains(m.Components, component) { log.Printf("component %s already in kustomization file", component) continue } diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addgenerator.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addgenerator.go index ee475d1b4..ab1d17230 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addgenerator.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addgenerator.go @@ -6,6 +6,7 @@ package add import ( "errors" "log" + "slices" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile" @@ -62,7 +63,7 @@ func (o *addGeneratorOptions) RunAddGenerator(fSys filesys.FileSystem) error { return err } for _, t := range o.generatorFilePaths { - if kustfile.StringInSlice(t, m.Generators) { + if slices.Contains(m.Generators, t) { log.Printf("generator %s already in kustomization file", t) continue } diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addmetadata.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addmetadata.go index eda017b76..25b1a5aac 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addmetadata.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addmetadata.go @@ -39,6 +39,7 @@ type addMetadataOptions struct { mapValidator func(map[string]string) error kind kindOfAdd labelsWithoutSelector bool + includeTemplates bool } // newCmdAddAnnotation adds one or more commonAnnotations to the kustomization file. @@ -83,6 +84,9 @@ func newCmdAddLabel(fSys filesys.FileSystem, v func(map[string]string) error) *c cmd.Flags().BoolVar(&o.labelsWithoutSelector, "without-selector", false, "using add labels without selector option", ) + cmd.Flags().BoolVar(&o.includeTemplates, "include-templates", false, + "include labels in templates (requires --without-selector)", + ) return cmd } @@ -112,6 +116,9 @@ func (o *addMetadataOptions) validateAndParse(args []string) error { if len(args) < 1 { return fmt.Errorf("must specify %s", o.kind) } + if !o.labelsWithoutSelector && o.includeTemplates { + return fmt.Errorf("--without-selector flag must be specified for --include-templates to work") + } m, err := util.ConvertSliceToMap(args, o.kind.String()) if err != nil { return err @@ -132,8 +139,7 @@ func (o *addMetadataOptions) addAnnotations(m *types.Kustomization) error { func (o *addMetadataOptions) addLabels(m *types.Kustomization) error { if o.labelsWithoutSelector { - m.Labels = append(m.Labels, types.Label{Pairs: make(map[string]string), IncludeSelectors: false}) - return o.writeToMap(m.Labels[len(m.Labels)-1].Pairs, label) + return o.writeToLabels(m, label) } if m.CommonLabels == nil { m.CommonLabels = make(map[string]string) @@ -143,10 +149,67 @@ func (o *addMetadataOptions) addLabels(m *types.Kustomization) error { func (o *addMetadataOptions) writeToMap(m map[string]string, kind kindOfAdd) error { for k, v := range o.metadata { - if _, ok := m[k]; ok && !o.force { - return fmt.Errorf("%s %s already in kustomization file", kind, k) + if err := o.writeToMapEntry(m, k, v, kind); err != nil { + return err } - m[k] = v } return nil } + +func (o *addMetadataOptions) writeToMapEntry(m map[string]string, k, v string, kind kindOfAdd) error { + if _, ok := m[k]; ok && !o.force { + return fmt.Errorf("%s %s already in kustomization file. Use --force to override.", kind, k) + } + m[k] = v + return nil +} + +func (o *addMetadataOptions) writeToLabels(m *types.Kustomization, kind kindOfAdd) error { + lbl := types.Label{ + Pairs: make(map[string]string), + IncludeSelectors: false, + IncludeTemplates: o.includeTemplates, + } + for k, v := range o.metadata { + if i, ok := o.findLabelKeyIndex(m, lbl, k); ok { + if err := o.writeToMapEntry(m.Labels[i].Pairs, k, v, kind); err != nil { + return err + } + continue + } + if i, ok := o.findLabelIndex(m, lbl); ok { + if err := o.writeToMapEntry(m.Labels[i].Pairs, k, v, kind); err != nil { + return err + } + continue + } + if err := o.writeToMap(lbl.Pairs, kind); err != nil { + return err + } + m.Labels = append(m.Labels, lbl) + } + return nil +} + +func (o *addMetadataOptions) matchLabelSettings(lbl1, lbl2 types.Label) bool { + return lbl1.IncludeSelectors == lbl2.IncludeSelectors && + lbl1.IncludeTemplates == lbl2.IncludeTemplates +} + +func (o *addMetadataOptions) findLabelIndex(m *types.Kustomization, lbl types.Label) (int, bool) { + for i, ml := range m.Labels { + if o.matchLabelSettings(ml, lbl) { + return i, true + } + } + return 0, false +} + +func (o *addMetadataOptions) findLabelKeyIndex(m *types.Kustomization, lbl types.Label, key string) (int, bool) { + if i, found := o.findLabelIndex(m, lbl); found { + if _, ok := m.Labels[i].Pairs[key]; ok { + return i, true + } + } + return 0, false +} diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addresource.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addresource.go index 529f2b119..3c63b4b13 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addresource.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addresource.go @@ -6,6 +6,7 @@ package add import ( "errors" "log" + "slices" "github.com/spf13/cobra" ldrhelper "sigs.k8s.io/kustomize/api/pkg/loader" @@ -73,7 +74,7 @@ func (o *addResourceOptions) RunAddResource(fSys filesys.FileSystem) error { for _, resource := range resources { if mf.GetPath() != resource { - if kustfile.StringInSlice(resource, m.Resources) { + if slices.Contains(m.Resources, resource) { log.Printf("resource %s already in kustomization file", resource) continue } diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addtransformer.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addtransformer.go index 288ee3f3e..ffc4aa77e 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addtransformer.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add/addtransformer.go @@ -6,6 +6,7 @@ package add import ( "errors" "log" + "slices" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile" @@ -62,7 +63,7 @@ func (o *addTransformerOptions) RunAddTransformer(fSys filesys.FileSystem) error return err } for _, t := range o.transformerFilePaths { - if kustfile.StringInSlice(t, m.Transformers) { + if slices.Contains(m.Transformers, t) { log.Printf("transformer %s already in kustomization file", t) continue } diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/fix/convert.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/fix/convert.go index f6c450803..04162d7c1 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/fix/convert.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/fix/convert.go @@ -7,6 +7,7 @@ import ( "bytes" "fmt" "path" + "slices" "strconv" "strings" @@ -55,11 +56,11 @@ func filesTouchedByKustomize(k *types.Kustomization, filepath string, fSys files files, err := fSys.ReadDir(r) if err == nil && len(files) > 0 { for _, file := range files { - if !stringInSlice(file, []string{ + if !slices.Contains([]string{ "kustomization.yaml", "kustomization.yml", "Kustomization", - }) { + }, file) { continue } @@ -223,7 +224,7 @@ func constructFieldOptions(value string, varString string) ([]*types.FieldOption return nil, fmt.Errorf("cannot convert all vars to replacements; %s is not delimited", varString) } delimiter = pre - index = indexOf(varString, strings.Split(value, delimiter)) + index = slices.Index(strings.Split(value, delimiter), varString) if index == -1 { // this should never happen return nil, fmt.Errorf("internal error: could not get index of var %s", varString) @@ -327,21 +328,3 @@ func setPlaceholderValue(varName string, files []string, fSys filesys.FileSystem } return nil } - -func stringInSlice(elem string, slice []string) bool { - for i := range slice { - if slice[i] == elem { - return true - } - } - return false -} - -func indexOf(varName string, slice []string) int { - for i := range slice { - if slice[i] == varName { - return i - } - } - return -1 -} diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removebuildmetadata.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removebuildmetadata.go index de27de0a1..58e30488c 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removebuildmetadata.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removebuildmetadata.go @@ -4,6 +4,8 @@ package remove import ( + "slices" + "github.com/spf13/cobra" "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile" "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/util" @@ -59,7 +61,7 @@ func (o *removeBuildMetadataOptions) RunRemoveBuildMetadata(fSys filesys.FileSys } var newOptions []string for _, opt := range m.BuildMetadata { - if !kustfile.StringInSlice(opt, o.buildMetadataOptions) { + if !slices.Contains(o.buildMetadataOptions, opt) { newOptions = append(newOptions, opt) } } diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removeconfigmap.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removeconfigmap.go index 7eeedb7d0..056db08e9 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removeconfigmap.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removeconfigmap.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "log" + "slices" "strings" "github.com/spf13/cobra" @@ -88,7 +89,7 @@ func (o *removeConfigMapOptions) RunRemoveConfigMap(fSys filesys.FileSystem) err remainingConfigMaps := make([]types.ConfigMapArgs, 0, len(m.ConfigMapGenerator)) for _, currentConfigMap := range m.ConfigMapGenerator { - if kustfile.StringInSlice(currentConfigMap.Name, o.configMapNamesToRemove) && + if slices.Contains(o.configMapNamesToRemove, currentConfigMap.Name) && util.NamespaceEqual(currentConfigMap.Namespace, o.namespace) { foundConfigMaps[currentConfigMap.Name] = struct{}{} continue diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removemetadata.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removemetadata.go index 3c34916fc..8d04062c5 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removemetadata.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removemetadata.go @@ -40,7 +40,7 @@ type removeMetadataOptions struct { kind kindOfAdd } -// newCmdRemoveLabel removes one or more commonAnnotations from the kustomization file. +// newCmdRemoveAnnotation removes one or more commonAnnotations from the kustomization file. func newCmdRemoveAnnotation(fSys filesys.FileSystem, v func([]string) error) *cobra.Command { var o removeMetadataOptions o.kind = label diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removeresource.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removeresource.go index fc76943d0..1fdbf79be 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removeresource.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removeresource.go @@ -6,6 +6,7 @@ package remove import ( "errors" "path/filepath" + "slices" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/api/konfig" @@ -73,7 +74,7 @@ func (o *removeResourceOptions) RunRemoveResource(fSys filesys.FileSystem) error newResources := make([]string, 0, len(m.Resources)) for _, resource := range m.Resources { - if kustfile.StringInSlice(resource, resources) { + if slices.Contains(resources, resource) { continue } newResources = append(newResources, resource) diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removesecret.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removesecret.go index 89a8af230..d648051ed 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removesecret.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removesecret.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "log" + "slices" "strings" "github.com/spf13/cobra" @@ -89,7 +90,7 @@ func (o *removeSecretOptions) RunRemoveSecret(fSys filesys.FileSystem) error { remainingSecrets := make([]types.SecretArgs, 0, len(m.SecretGenerator)) for _, currentSecret := range m.SecretGenerator { - if kustfile.StringInSlice(currentSecret.Name, o.secretNamesToRemove) && + if slices.Contains(o.secretNamesToRemove, currentSecret.Name) && util.NamespaceEqual(currentSecret.Namespace, o.namespace) { foundSecrets[currentSecret.Name] = struct{}{} continue diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removetransformer.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removetransformer.go index c8e188cec..7de87256f 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removetransformer.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove/removetransformer.go @@ -5,6 +5,7 @@ package remove import ( "errors" + "slices" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/api/konfig" @@ -72,7 +73,7 @@ func (o *removeTransformerOptions) RunRemoveTransformer(fSys filesys.FileSystem) newTransformers := make([]string, 0, len(m.Transformers)) for _, transformer := range m.Transformers { - if kustfile.StringInSlice(transformer, transformers) { + if slices.Contains(transformers, transformer) { continue } newTransformers = append(newTransformers, transformer) diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile/kustomizationfile.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile/kustomizationfile.go index 4804b5691..480eac473 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile/kustomizationfile.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile/kustomizationfile.go @@ -40,6 +40,7 @@ func determineFieldOrder() []string { ordered := []string{ "MetaData", + "SortOptions", "Resources", "Bases", "NamePrefix", @@ -65,6 +66,7 @@ func determineFieldOrder() []string { "Configurations", "Generators", "Transformers", + "Validators", "Components", "OpenAPI", "BuildMetadata", @@ -189,16 +191,6 @@ func (mf *kustomizationFile) Write(kustomization *types.Kustomization) error { return mf.fSys.WriteFile(mf.path, data) } -// StringInSlice returns true if the string is in the slice. -func StringInSlice(str string, list []string) bool { - for _, v := range list { - if v == str { - return true - } - } - return false -} - func (mf *kustomizationFile) parseCommentedFields(content []byte) error { buffer := bytes.NewBuffer(content) var comments [][]byte diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/internal/util/validate.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/internal/util/validate.go index bf041b50f..0fde88f78 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/internal/util/validate.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/internal/util/validate.go @@ -6,10 +6,10 @@ package util import ( "errors" "fmt" + "slices" "strings" "sigs.k8s.io/kustomize/api/types" - "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile" ) type BuildMetadataValidator struct{} @@ -23,7 +23,7 @@ func (b *BuildMetadataValidator) Validate(args []string) ([]string, error) { } opts := strings.Split(args[0], ",") for _, opt := range opts { - if !kustfile.StringInSlice(opt, types.BuildMetadataOptions) { + if !slices.Contains(types.BuildMetadataOptions, opt) { return nil, fmt.Errorf("invalid buildMetadata option: %s", opt) } } diff --git a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/localize/localize.go b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/localize/localize.go index d557ebceb..5aaa9bb41 100644 --- a/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/localize/localize.go +++ b/vendor/sigs.k8s.io/kustomize/kustomize/v5/commands/localize/localize.go @@ -4,10 +4,14 @@ package localize import ( + "bytes" "log" + "path/filepath" "github.com/spf13/cobra" lclzr "sigs.k8s.io/kustomize/api/krusty/localizer" + "sigs.k8s.io/kustomize/kustomize/v5/commands/build" + "sigs.k8s.io/kustomize/kyaml/copyutil" "sigs.k8s.io/kustomize/kyaml/errors" "sigs.k8s.io/kustomize/kyaml/filesys" ) @@ -20,23 +24,26 @@ type arguments struct { } type flags struct { - scope string + scope string + noVerify bool } // NewCmdLocalize returns a new localize command. func NewCmdLocalize(fs filesys.FileSystem) *cobra.Command { var f flags + var buildBuffer bytes.Buffer + buildCmd := build.NewCmdBuild(fs, &build.Help{}, &buildBuffer) cmd := &cobra.Command{ Use: "localize [target [destination]]", Short: "[Alpha] Creates localized copy of target kustomization root at destination", - Long: `[Alpha] Creates copy of target kustomization directory or -versioned URL at destination, where remote references in the original + Long: `[Alpha] Creates copy of target kustomization directory or +versioned URL at destination, where remote references in the original are replaced by local references to the downloaded remote content. -If target is not specified, the current working directory will be used. -Destination is a path to a new directory in an existing directory. If -destination is not specified, a new directory will be created in the current -working directory. +If target is not specified, the current working directory will be used. +Destination is a path to a new directory in an existing directory. If +destination is not specified, a new directory will be created in the current +working directory. For details, see: https://kubectl.docs.kubernetes.io/references/kustomize/cmd/ @@ -46,7 +53,7 @@ alphabetizes kustomization fields in the localized copy. `, Example: ` # Localize the current working directory, with default scope and destination -kustomize localize +kustomize localize # Localize some local directory, with scope and default destination kustomize localize /home/path/scope/target --scope /home/path/scope @@ -62,6 +69,30 @@ kustomize localize https://github.com/kubernetes-sigs/kustomize//api/krusty/test if err != nil { return errors.Wrap(err) } + + if !f.noVerify { + originalBuild, err := runBuildCmd(buildBuffer, buildCmd, args.target) + if err != nil { + return errors.Wrap(err) + } + + buildDst := dst + if f.scope != "" && f.scope != args.target { + buildDst = filepath.Join(dst, filepath.Base(args.target)) + } + + localizedBuild, err := runBuildCmd(buildBuffer, buildCmd, buildDst) + if err != nil { + return errors.Wrap(err) + } + + if localizedBuild != originalBuild { + copyutil.PrettyFileDiff(originalBuild, localizedBuild) + log.Fatalf("VERIFICATION FAILED: `kustomize build` for %s and %s are different after localization.\n", args.target, dst) + } + log.Printf("VERIFICATION SUCCESS: `kustomize build` for %s and %s are the same after localization.\n", args.target, dst) + } + log.Printf("SUCCESS: localized %q to directory %s\n", args.target, dst) return nil }, @@ -74,6 +105,12 @@ kustomize localize https://github.com/kubernetes-sigs/kustomize//api/krusty/test Cannot specify for remote targets, as scope is by default the containing repo. If not specified for local target, scope defaults to target. `) + cmd.Flags().BoolVar(&f.noVerify, + "no-verify", + false, + `Does not verify that the outputs of kustomize build for target and newDir are the same after localization. + If not specified, this flag defaults to false and will run kustomize build. + `) return cmd } @@ -92,3 +129,16 @@ func matchArgs(rawArgs []string) arguments { } return args } + +func runBuildCmd(buffer bytes.Buffer, cmd *cobra.Command, folder string) (buildOutput string, err error) { + buffer.Reset() + buildErr := cmd.RunE(cmd, []string{folder}) + if buildErr != nil { + log.Printf("If your target directory requires flags to build: \n"+ + "1. Add executable permissions for the downloaded exec binaries in '%s'. \n"+ + "2. Run kustomize build with the necessary flags and self-verify the outputs.", folder) + return "", errors.Wrap(buildErr) + } + + return buffer.String(), nil +} diff --git a/vendor/sigs.k8s.io/kustomize/kyaml/copyutil/copyutil.go b/vendor/sigs.k8s.io/kustomize/kyaml/copyutil/copyutil.go new file mode 100644 index 000000000..98cb8314d --- /dev/null +++ b/vendor/sigs.k8s.io/kustomize/kyaml/copyutil/copyutil.go @@ -0,0 +1,197 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +// The copyutil package contains libraries for copying directories of configuration. +package copyutil + +import ( + "bytes" + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/sergi/go-diff/diffmatchpatch" + "sigs.k8s.io/kustomize/kyaml/errors" + "sigs.k8s.io/kustomize/kyaml/filesys" + "sigs.k8s.io/kustomize/kyaml/sets" +) + +// CopyDir copies a src directory to a dst directory. CopyDir skips copying the .git directory from the src. +func CopyDir(fSys filesys.FileSystem, src string, dst string) error { + return errors.Wrap(fSys.Walk(src, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + // don't copy the .git dir + if path != src { + rel := strings.TrimPrefix(path, src) + if IsDotGitFolder(rel) { + return nil + } + } + + // path is an absolute path, rather than a path relative to src. + // e.g. if src is /path/to/package, then path might be /path/to/package/and/sub/dir + // we need the path relative to src `and/sub/dir` when we are copying the files to dest. + copyTo := strings.TrimPrefix(path, src) + + // make directories that don't exist + if info.IsDir() { + return errors.Wrap(fSys.MkdirAll(filepath.Join(dst, copyTo))) + } + + // copy file by reading and writing it + b, err := fSys.ReadFile(filepath.Join(src, copyTo)) + if err != nil { + return errors.Wrap(err) + } + err = fSys.WriteFile(filepath.Join(dst, copyTo), b) + if err != nil { + return errors.Wrap(err) + } + + return nil + })) +} + +// Diff returns a list of files that differ between the source and destination. +// +// Diff is guaranteed to return a non-empty set if any files differ, but +// this set is not guaranteed to contain all differing files. +func Diff(sourceDir, destDir string) (sets.String, error) { + // get set of filenames in the package source + upstreamFiles := sets.String{} + err := filepath.Walk(sourceDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + // skip git repo if it exists + if IsDotGitFolder(path) { + return nil + } + + upstreamFiles.Insert(strings.TrimPrefix(strings.TrimPrefix(path, sourceDir), string(filepath.Separator))) + return nil + }) + if err != nil { + return sets.String{}, err + } + + // get set of filenames in the cloned package + localFiles := sets.String{} + err = filepath.Walk(destDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + // skip git repo if it exists + if IsDotGitFolder(path) { + return nil + } + + localFiles.Insert(strings.TrimPrefix(strings.TrimPrefix(path, destDir), string(filepath.Separator))) + return nil + }) + if err != nil { + return sets.String{}, err + } + + // verify the source and cloned packages have the same set of filenames + diff := upstreamFiles.SymmetricDifference(localFiles) + + // verify file contents match + for _, f := range upstreamFiles.Intersection(localFiles).List() { + fi, err := os.Stat(filepath.Join(destDir, f)) + if err != nil { + return diff, err + } + if fi.Mode().IsDir() { + // already checked that this directory exists in the local files + continue + } + + // compare upstreamFiles + b1, err := os.ReadFile(filepath.Join(destDir, f)) + if err != nil { + return diff, err + } + b2, err := os.ReadFile(filepath.Join(sourceDir, f)) + if err != nil { + return diff, err + } + if !bytes.Equal(b1, b2) { + fmt.Println(PrettyFileDiff(string(b1), string(b2))) + diff.Insert(f) + } + } + // return the differing files + return diff, nil +} + +// IsDotGitFolder checks if the provided path is either the .git folder or +// a file underneath the .git folder. +func IsDotGitFolder(path string) bool { + cleanPath := filepath.ToSlash(filepath.Clean(path)) + for _, c := range strings.Split(cleanPath, "/") { + if c == ".git" { + return true + } + } + return false +} + +// PrettyFileDiff takes the content of two files and returns the pretty diff +func PrettyFileDiff(s1, s2 string) string { + dmp := diffmatchpatch.New() + wSrc, wDst, warray := dmp.DiffLinesToRunes(s1, s2) + diffs := dmp.DiffMainRunes(wSrc, wDst, false) + diffs = dmp.DiffCharsToLines(diffs, warray) + return dmp.DiffPrettyText(diffs) +} + +// SyncFile copies file from src file path to a dst file path by replacement +// deletes dst file if src file doesn't exist +func SyncFile(src, dst string) error { + srcFileInfo, err := os.Stat(src) + if err != nil { + // delete dst if source doesn't exist + if err = deleteFile(dst); err != nil { + return err + } + return nil + } + + input, err := os.ReadFile(src) + if err != nil { + return err + } + + var filePerm os.FileMode + + // get the destination file perm if file exists + dstFileInfo, err := os.Stat(dst) + if err != nil { + // get source file perm if destination file doesn't exist + filePerm = srcFileInfo.Mode().Perm() + } else { + filePerm = dstFileInfo.Mode().Perm() + } + + err = os.WriteFile(dst, input, filePerm) + if err != nil { + return err + } + + return nil +} + +// deleteFile deletes file from path, returns no error if file doesn't exist +func deleteFile(path string) error { + if _, err := os.Stat(path); err != nil { + // return nil if file doesn't exist + return nil + } + return os.Remove(path) +}