I'm not a regex wizard :P and I think asking for some help does not hurt.
I was wondering to extract an IP address from a k8s service (traefik), in order to use the IP Address string in my code.
An array of strings will be available and I just need the LoadBalancer Ingress
IP address: 10.0.0.115
-- the rest, we can ignore it.
Here's a small example what I'm trying to perform:
const log = [
'Name: traefik',
'Namespace: kube-system',
'Labels: app.kubernetes.io/instance=traefik',
' app.kubernetes.io/managed-by=Helm',
' app.kubernetes.io/name=traefik',
' helm.sh/chart=traefik-10.9.100',
'Annotations: meta.helm.sh/release-name: traefik',
' meta.helm.sh/release-namespace: kube-system',
'Selector: app.kubernetes.io/instance=traefik,app.kubernetes.io/name=traefik',
'Type: LoadBalancer',
'IP Family Policy: SingleStack',
'IP Families: IPv4',
'IP: 10.43.251.46',
'IPs: 10.43.251.46',
'LoadBalancer Ingress: 10.0.0.115',
'Port: web 80/TCP',
'TargetPort: web/TCP',
'NodePort: web 31729/TCP',
'Endpoints: 10.42.0.20:8000',
'Port: websecure 443/TCP',
'TargetPort: websecure/TCP',
'NodePort: websecure 31708/TCP',
'Endpoints: 10.42.0.20:8443',
'Session Affinity: None',
'External Traffic Policy: Cluster',
'Events: <none>',
''
];
const regex = new RegExp(/(Ingress:)(\s.*\d)/g);
const result = regex.exec(log);
console.log(result[2]);
The regex actually matches, but it brings everything after the expected string:
" 10.0.0.115,Port: web 80/TCP,TargetPort: web/TCP,NodePort: web 31729/TCP,Endpoints: 10.42.0.20:8000,Port: websecure 443/TCP,TargetPort: websecure/TCP,NodePort: websecure 31708/TCP,Endpoints: 10.42.0.20:8443"
Expected output: 10.0.0.115
Update: Here's the regex test https://regex101.com/r/fKtMH7/1
I tried another methods to accomplish the same result, but regex seems to be the reliable one.
Any help will be very appreciated.
Thanks
CodePudding user response:
Well, since you want to do it with regex, one solution would be to find to group the Ingress:
and then the IP address.
const log = [
'Name: traefik',
'Namespace: kube-system',
'Labels: app.kubernetes.io/instance=traefik',
' app.kubernetes.io/managed-by=Helm',
' app.kubernetes.io/name=traefik',
' helm.sh/chart=traefik-10.9.100',
'Annotations: meta.helm.sh/release-name: traefik',
' meta.helm.sh/release-namespace: kube-system',
'Selector: app.kubernetes.io/instance=traefik,app.kubernetes.io/name=traefik',
'Type: LoadBalancer',
'IP Family Policy: SingleStack',
'IP Families: IPv4',
'IP: 10.43.251.46',
'IPs: 10.43.251.46',
'LoadBalancer Ingress: 10.0.0.115',
'Port: web 80/TCP',
'TargetPort: web/TCP',
'NodePort: web 31729/TCP',
'Endpoints: 10.42.0.20:8000',
'Port: websecure 443/TCP',
'TargetPort: websecure/TCP',
'NodePort: websecure 31708/TCP',
'Endpoints: 10.42.0.20:8443',
'Session Affinity: None',
'External Traffic Policy: Cluster',
'Events: <none>',
''
];
serviceRegex = /(LoadBalancer Ingress:)\s /
ipRegex = /((?:[0-9]{1,3}\.){3}[0-9]{1,3})/
const regex = new RegExp(`${serviceRegex.source}${ipRegex.source}`);
const result = regex.exec(log);
console.log(result[2]);
Of course you can write a better regex for the IP address, this is just quick example
CodePudding user response:
const result = JSON.parse(log.join(','));
console.log(result['LoadBalancer Ingress']);
I haven't tested this; however, this is a solid direction to take.