#include #include #include #include #include int main(int argc, char *argv[]) { if (argc < 6) { fprintf(stderr, "Usage:\t %s sourceip sourceport desinationip " "destinationport protocol [-o outputfile]\n", argv[0]); fprintf(stderr, "\tor %s sourcepod sourceport desinationpod " "destinationport protocol [-o outputfile]\n", argv[0]); fprintf(stderr, "\nExample:\t%s nginx 80 access 5978 TCP " "-o test.yaml\n", argv[0]); fprintf(stderr, "\t\t%s nginx 80 access 5978 TCP\n", argv[0]); return EX_USAGE; } int opt; char output[200] = ""; while ((opt = getopt(argc, argv, "o:")) != -1) { switch (opt) { case 'o': strcpy(output, optarg); break; } } if (strlen(output) == 0) { strcpy(output, "ingress-egress-nginx.yaml"); } char *sourceaddr = argv[optind]; int sourceport; sscanf(argv[optind + 1], "%d", &sourceport); char *destaddr = argv[optind + 2]; int destport; sscanf(argv[optind + 3], "%d", &destport); char *protocol = argv[optind + 4]; // printf( // "Source address: %s\nSource port: %d\nDestination address: " // "%s\nDestination port: %d\nProtocol: %s\nOutput: %s\n", // sourceaddr, sourceport, destaddr, destport, protocol, output); const char *template = "apiVersion: networking.k8s.io/v1\n" "kind: NetworkPolicy\n" "metadata:\n" " name: test-network-policy\n" " namespace: policy-demo\n" "spec:\n" " podSelector:\n" " matchLabels:\n" " run: %s\n" " ingress:\n" " - from:\n" " - ipBlock:\n" " cidr: 172.17.0.0/16\n" " - podSelector:\n" " matchLabels:\n" " run: %s\n" " ports:\n" " - protocol: %s\n" " port: %d\n" " egress:\n" " - to:\n" " - ipBlock:\n" " cidr: 10.0.0.0/24\n" " ports:\n" " - protocol: %s\n" " port: %d\n"; // printf("----------TEMPLATE----------\n"); // printf(template, sourceaddr, destaddr, protocol, sourceport, // protocol, destport); printf("\n----------------------------\n"); FILE *outputFile = fopen(output, "w"); fprintf(outputFile, template, sourceaddr, destaddr, protocol, sourceport, protocol, destport); fclose(outputFile); char cmd[100]; sprintf(cmd, "kubectl apply -f ./%s", output); printf("%s\n", cmd); system(cmd); return 0; }