Access Strimzi kafka through python #5455
Replies: 1 comment 3 replies
-
I have no idea how to do it in Python. In Java we have an But what I can do, is I can tell you about the error ... you should not use the entity operator service account. You should create your own service account for it with your own rights. The error you have here is basically just saying that with the entity operator service account you cannot create a Job resource because of missing RBAC (and because as I said above, the BatchV1API will create a Job and not a KafkaTopic). Also, keep in mind that the Topic Operator is special in that it works in both directions. So you can also just create the topic in Kafka and it will create the KafkaTopic resource itself. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
We are planning to create dynamic topics, connect, connector through python program. In order to access through code, we are getting ip address, token, certificate from the below service account tokens:
(currently cluster strimzi kafka is running in GKE cluster)
Using below commands to get token and certificates
CAcertifcates:
kubectl -n kafkanew get secrets my-cluster-entity-operator-token-9cst7 -o jsonpath="{['data']['ca.crt']}"
Token:
kubectl -n kafkanew get secrets my-cluster-entity-operator-token-9cst7 -o jsonpath={.data.token} | base64 -d
Python code for creating topics:
from kubernetes import client
import base64
from tempfile import NamedTemporaryFile
import os
import yaml
from os import path
def main():
try:
host_url = 'https://35.224.26.16'
cacert = 'LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURLekNDQWhPZ0F3SUJBZ0lSQU1pSHRSSTY5Tm95ZFmNtemhjQmJtUGJCNFhNUGs3SFZoNEZDdUhBCllQdDV2eUpLVTBHSnpJeWUybjM1SlVQc1ljR1J1Yk5JajduNEM4RWg5L2NyK3htVGUwVDRDaE83NUVUckl5WkgKT3dtMnRaZVR4TVhOR0h3RnRRTUZZbEtoU20rb2tCekErRW5uZ1JlRTh6c2hEMEUvMUVRN0tUWExKNEdUUHdYVwpzUG5iNHRWREtXTWVaUHhNYnkwOFp1bHJQRWcyVjZvUFd6TjA4cyszRVVhMnB1L041dHhXcE1Yd0MxbTFDd3htCmFMZ05kekRZakd5UHQ5S0FpTUZKOGN1RDFOaEpQdUd3S0RWR3BnL2Q3ZUVaMFFJREFRQUJvM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kaTopic
metadata:
name: my-pythontesttopic
labels:
strimzi.io/cluster: my-cluster
spec:
partitions: 3
replicas: 3
config:
retention.ms: 7200000
segment.bytes: 1073741824
while executing above python code I am getting below error message:
read parameters
read all certificates
try to apply ymal file
body{'apiVersion': 'kafka.strimzi.io/v1beta2', 'kind': 'KafkaTopic', 'metadata': {'name': 'my-pythontesttopic', 'labels': {'strimzi.io/cluster': 'my-cluster'}}, 'spec': {'partitions': 3, 'replicas': 3, 'config': {'retention.ms': 7200000, 'segment.bytes': 1073741824}}}
some error in code(403)
Reason: Forbidden
HTTP response headers: HTTPHeaderDict({'Audit-Id': 'b44c7f92-7d24-41b8-93ae-6caecdd74ebf', 'Cache-Control': 'no-cache, private', 'Content-Type': 'application/json', 'X-Content-Type-Options': 'nosniff', 'Date': 'Thu, 19 Aug 2021 11:28:06 GMT', 'Content-Length': '387'})
HTTP response body: {
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Failure",
"message": "jobs.batch is forbidden: User "system:serviceaccount:kafkanew:my-cluster-entity-operator" cannot create resource "jobs" in API group "batch" in the namespace "kafkanew"",
"reason": "Forbidden",
"details": {
"group": "batch",
"kind": "jobs"
},
"code": 403
}
Could you suggest how to create/modify strimzi kafka through Kubernetes API. Is there special role I need to create it? in order to create topic, connector and connect through python?
Beta Was this translation helpful? Give feedback.
All reactions