/
apiversions.go
95 lines (87 loc) · 2.56 KB
/
apiversions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright © 2018 NAME HERE <jbonds@jbvm.io>
//
// 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 kafka
import (
"sort"
"github.com/Shopify/sarama"
"github.com/jbvmio/kafkactl/cli/cx"
"github.com/jbvmio/kafkactl/cli/x/out"
kafkactl "github.com/jbvmio/kafka"
)
// APIVersion describes an API Version Key and its Max Version.
type APIVersion struct {
Name string
Key int16
MaxVersion int16
}
func GetAPIVersions() []APIVersion {
var apiVersions []APIVersion
apiVers, err := client.GetAPIVersions()
handleC("Error: %v", err)
for k := range apiVers {
api := APIVersion{
Name: kafkactl.APIDescriptions[k],
Key: k,
MaxVersion: apiVers[k],
}
apiVersions = append(apiVersions, api)
}
sort.Slice(apiVersions, func(i, j int) bool {
return apiVersions[i].Key < apiVersions[j].Key
})
return apiVersions
}
func findKafkaVersion(config *sarama.Config, context *cx.Context) string {
bootStrap, err := kafkactl.ReturnFirstValid(config, context.Brokers...)
if err != nil {
out.Failf("Error connecting to Kafka: %v", err)
}
apiVer, err := kafkactl.BrokerAPIVersions(config, bootStrap)
if err != nil {
if verbose {
kafkactl.Warnf("%v", err)
}
}
return getKafkaVersion(apiVer)
}
// Release Notes:
// https://downloads.apache.org/kafka/
func getKafkaVersion(apiKeys map[int16]int16) string {
_, ok23 := apiKeys[kafkactl.APIKeyIncrementalAlterConfigs]
_, ok24 := apiKeys[kafkactl.APIKeyOffsetDelete]
_, ok26 := apiKeys[kafkactl.APIKeyDescribeClientQuotas]
switch {
case ok26:
return "2.6.0"
case ok24:
return "2.4.0"
case ok23:
return "2.3.0"
case apiKeys[kafkactl.APIKeyOffsetForLeaderEpoch] >= 2:
return "2.1.0"
case apiKeys[kafkactl.APIKeyOffsetForLeaderEpoch] == 1:
return "2.0.0"
case apiKeys[kafkactl.APIKeyFetch] == 7:
return "1.1.0"
case apiKeys[kafkactl.APIKeyFetch] == 6:
return "1.0.0"
case apiKeys[kafkactl.APIKeyFetch] == 5:
return "0.11.0.0"
case apiKeys[kafkactl.APIKeyUpdateMetadata] == 3:
return "0.10.2.0"
case apiKeys[kafkactl.APIKeyFetch] == 3:
return "0.10.1.0"
}
return "UnknownVersion"
}